当前位置:网站首页>SQL optimization best practices
SQL optimization best practices
2022-04-23 06:08:00 【New ape and horse】
Catalog
1 Business logic optimization , Reduce the scope of data query
2 What you take is what you use , Avoid select *
3 Establish a reasonable index , Good at using indexes
4 Avoid doing operations or function operations on index columns
5 avoid like Use , Be sure to use , Field plus index
6 It is recommended to use join Substitute subqueries
9 Make good use of force Field
sql The essence of optimization is to reduce io、cpu The consumption of resources , Give Way sql Faster execution , Finally meet our performance requirements .
Here's what we've been through sql Summary of optimization practice :
1 Business logic optimization , Reduce the scope of data query
# With in For example , Alibaba protocol recommends that batch queries be controlled at 200 within
select * from Table name where id in (1,3,,5,7 ......)
2 What you take is what you use , Avoid select *
What you take is what you use , That is, we can take whatever fields we need , avoid select * .
Why avoid select * Well ? Suppose our business table has 50 A field ,select * It means to take this... At one time 50 A field , this 50 The transmission of two fields in the network takes up a lot of bandwidth , Affect transmission speed .
3 Establish a reasonable index , Good at using indexes
- Select the column with high field discrimination in the table to establish the index .
- Priority is given to where and order by Index the columns involved .
# name stay user High discrimination of tables , You can give name Add index
select name from user where name = " Zhang San ";
# age Index , because age Field order , It can reduce the consumption of sorting
select name from user order by age limit 20;
# (name,age) Set up a joint index
select name,age from user where name = " Zhang San " order by age limit 20;
4 Avoid doing operations or function operations on index columns
Do operations or function operations on index columns , Will result in index invalidation .
select id from user where age/2 = 5;
5 avoid like Use , Be sure to use , Field plus index
select id from user where name like 'jim%'
6 It is recommended to use join Substitute subqueries
# Don't suggest
select a.name from a where a.id in (select rid from b)
# Suggest
select a.name from a left join b on a.id = b.rid;
7 Avoid using join
In principle, avoid using join, If it must be used , It is suggested that the associated fields should have indexes , The small table is preferred as the driving table .
select a.name from a join b on a.id = b.rid where a.id = 9;
8 DML Be short
So-called DML Be short , What does that mean , for instance , We need to update 1 Ten thousand data , We can divide it into 10 Secondary update , Every time 1 A thousand , perform 10 Time , Why do you want to do this ? An update if you hold the lock for a long time , It's blocking other sql, Affect system performance , And if it is divided into several times , It will greatly reduce the probability of lock collision .
update user set b = 1 where id between 1 and 1000;
update user set b = 1 where id between 1001 and 2000;
......
update user set b = 1 where id between 9001 and 10000;
9 Make good use of force Field
You may have met sql There is no problem with the right index , It can be used at this time force Give Way sql Go to the index .
select a.name from user force index( Indexes );
Don't abuse force, Only in sql When there is no index , And we need to make sql Use when indexing .
版权声明
本文为[New ape and horse]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220533487616.html
边栏推荐
- 编写一个自己的 RedisTemplate
- Custom exception class
- You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
- 去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
- JSP syntax and JSTL tag
- PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
- Numpy common function table sorting of data processing
- Rsync for file server backup
- Software architecture design - software architecture style
- DBCP usage
猜你喜欢
Latex快速入门
Pytorch学习记录(十二):学习率衰减+正则化
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
Pytorch學習記錄(十三):循環神經網絡((Recurrent Neural Network)
Pytorch Learning record (XIII): Recurrent Neural Network
域内用户访问域外samba服务器用户名密码错误
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
随机推荐
RedHat6之smb服务访问速度慢解决办法记录
EditorConfig
Illustrate the significance of hashcode
Multithreading and high concurrency (3) -- synchronized principle
In depth source code analysis servlet first program
编写一个自己的 RedisTemplate
JDBC tool class encapsulation
Software architecture design - software architecture style
数字图像处理基础(冈萨雷斯)一
JDBC operation transaction
container
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
Kingdee EAS "general ledger" system calls "de posting" button
开发环境 EAS登录 license 许可修改
Pytorch learning record (IX): convolutional neural network in pytorch
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code