当前位置:网站首页>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
边栏推荐
- RedHat realizes keyword search in specific text types under the directory and keyword search under VIM mode
- Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
- LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
- Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
- Contrôle automatique (version Han min)
- filebrowser实现私有网盘
- Filebrowser realizes private network disk
- 線性代數第二章-矩陣及其運算
- 给yarn配置国内镜像加速器
- You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
猜你喜欢
Opensips (1) -- detailed process of installing opensips
Illustrate the significance of hashcode
Filebrowser realizes private network disk
Software architecture design - software architecture style
给yarn配置国内镜像加速器
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Pytoch learning record (x): data preprocessing + batch normalization (BN)
Manually delete registered services on Eureka
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
JVM series (3) -- memory allocation and recycling strategy
随机推荐
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
2. Devops sonar installation
给yarn配置国内镜像加速器
Pytoch learning record (x): data preprocessing + batch normalization (BN)
A sharp tool to improve work efficiency
Linear algebra Chapter 1 - determinant
Pytorch学习记录(七):处理数据和训练模型的技巧
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
Framework analysis 1 Introduction to system architecture
Create enterprise mailbox account command
DBCP usage
線性代數第二章-矩陣及其運算
EditorConfig
自動控制(韓敏版)
PyEMD安装及简单使用
Class loading and classloader understanding
Explain of MySQL optimization
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)