当前位置:网站首页>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
边栏推荐
- Fundamentals of digital image processing (Gonzalez) I
- Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
- sklearn之 Gaussian Processes
- Pytoch -- data loading and processing
- PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
- Anaconda
- Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
- Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
- Configure domestic image accelerator for yarn
- 图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
猜你喜欢

PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码

Pytorch learning record (XII): learning rate attenuation + regularization

Pyqy5 learning (III): qlineedit + qtextedit

lambda expressions

Ptorch learning record (XIII): recurrent neural network

In depth source code analysis servlet first program

Conda 虚拟环境管理(创建、删除、克隆、重命名、导出和导入)

线性代数第二章-矩阵及其运算

Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison

Opensips (1) -- detailed process of installing opensips
随机推荐
umi官网yarn create @umijs/umi-app 报错:文件名、目录名或卷标语法不正确
Anaconda
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
The user name and password of users in the domain accessing the samba server outside the domain are wrong
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
数字图像处理基础(冈萨雷斯)一
图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
Pytorch learning record (IX): convolutional neural network in pytorch
Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
Pytorch学习记录(十二):学习率衰减+正则化
Latex quick start
In depth understanding of the relationship between dncblevel and noise denoising in the paper
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Numpy common function table sorting of data processing
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
Pytorch学习记录(四):参数初始化
PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
Create binary tree
Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package