当前位置:网站首页>19 MySQL optimizations commonly used in projects
19 MySQL optimizations commonly used in projects
2022-06-23 23:32:00 【androidstarjack】
Click on the top “ Terminal R & D department ”
Set to “ Star standard ”, Master more database knowledge with you 
author | zhangqh
link | segmentfault.com/a/1190000012155267
About MySQL An optimization method , There are many materials and methods on the Internet , But many are of uneven quality , Some summaries are not in place , The content is miscellaneous . Occasionally see SF, Found this article , It's a classic summary , I hope it will help you in your future development . Today's article mentioned 19 A common MySQL An optimization method .
1、EXPLAIN
do MySQL Optimize , We need to make good use of EXPLAIN see SQL Implementation plan .
Here's a simple example , mark (1、2、3、4、5) The data we need to focus on :

type Column , Connection type . A good SQL The statement must at least reach range Level . Put an end to it all Level .
key Column , Index name used . If index is not selected , The value is NULL. You can use forced indexing .
key_len Column , Index length .
rows Column , Number of scanning lines . This is an estimate .
extra Column , Detailed instructions . Be careful , Common unfriendly values , as follows :Using filesort,Using temporary.
2、SQL In the sentence IN Should not contain too many values
MySQL about IN The corresponding optimization has been made , the IN All constants in are stored in an array , And this array is ordered . But if there are more values , The consumption is also relatively large . Another example is :select id from t where num in(1,2,3) For continuous values , It works between Don't use in 了 ; Or use connections to replace .
3、SELECT The statement must indicate the field name
SELECT* Add a lot of unnecessary consumption (CPU、IO、 Memory 、 network bandwidth ); Increased the possibility of using overlay indexes ; When the table structure changes , Front break also needs to be updated . So the request is directly in select Followed by the field name .
Tips: You can search by wechat :Java Back end , Join our own communication group after following .
4、 When only one piece of data is needed , Use limit 1
This is to make EXPLAIN in type Column reach const type
5、 If the sort field does not use an index , Just sort as little as possible
6、 If there is no index for other fields in the constraint , Use less as far as possible or
or In the fields on both sides , If there is a non index field , Other conditions are not index fields , Will cause the query not to go through the index . A lot of times union all Or is it union( When necessary ) Instead of “or” Will get better results .
7、 As far as possible with union all Instead of union
union and union all The main difference is that the former needs to merge the result set before uniqueness filtering , This involves sorting , A large increase in CPU operation , Increase resource consumption and delay . Of course ,union all The precondition is that there is no duplicate data in the two result sets .
8、 Don't use ORDER BY RAND()
select id from `dynamic` order by rand() limit 1000;
above SQL sentence , It can be optimized to :
select id from `dynamic` t1 join (select rand() * (select max(id) from `dynamic`) as nid) t2 on t1.id > t2.nidlimit 1000;
9、 distinguish in and exists、not in and not exists
select * from surface A where id in (select id from surface B)
above SQL Statement equivalent
select * from surface A where exists(select * from surface B where surface B.id= surface A.id)
distinguish in and exists Mainly caused the change of driving sequence ( This is the key to performance change ), If it is exists, Then take the outer table as the driving table , Be interviewed first , If it is IN, Then execute the subquery first . therefore IN It is suitable for the case of large exterior and small interior surface ;EXISTS It is suitable for the case of small appearance and large inner surface .
About not in and not exists, Recommended not exists, It's not just about efficiency ,not in There may be a logical problem . How to write an alternative efficiently not exists Of SQL sentence ?
primary SQL sentence :
select colname … from A surface where a.id not in (select b.id from B surface )
efficient SQL sentence :
select colname … from A surface Left join B surface on where a.id = b.id where b.id is null
The result set is shown in the figure below ,A The watch is not in B Table data :
10、 Use reasonable paging methods to improve the efficiency of paging
select id,name from product limit 866613, 20
Use the above SQL When the statement is paginated , Maybe someone will find out , With the increase of table data , Use it directly limit Paging queries will be slower and slower .
The optimization method is as follows : You can take the maximum number of lines on the previous page id, And then according to this biggest id To limit the starting point of the next page . Than in this column , The biggest one on the previous page id yes 866612.SQL It can be written as follows :
select id,name from product where id> 866612 limit 20
11、 Segmented query
In some user selection pages , Maybe some users choose too large a time range , Causing slow queries . The main reason is that there are too many scan lines . It's time to go through the program , Query in sections , Loop traversal , Combine the results for presentation .
As shown in the figure below SQL sentence , Segment query can be used when the number of scanned rows is more than one million :

12、 To avoid the where Field in the clause null Value judgement
about null The engine will give up using the index and scan the whole table .
13、 Not recommended % Prefix fuzzy query
for example LIKE“%name” perhaps LIKE“%name%”, This kind of query will lead to index failure and full table scanning . But you can use LIKE “name%”.
How to query %name%?
As shown in the figure below , Although give to secret Field added index , But in explain It turns out that it didn't use :

So how to solve this problem , answer : Use full text indexing .
We often use select id,fnum,fdst from dynamic_201606 where user_name like '%zhangsan%'; . Such a statement , Ordinary index can't satisfy the query demand . Fortunately, in MySQL in , There is a full-text index to help us .
To create a full-text index SQL Grammar is :
ALTER TABLE `dynamic_201606` ADD FULLTEXT INDEX `idx_user_name` (`user_name`);
Using full-text indexing SQL The sentence is :
select id,fnum,fdst from dynamic_201606 where match(user_name) against('zhangsan' in boolean mode);
Be careful : Before you need to create a full-text index , Please contact the DBA Determine if you can create . At the same time, we need to pay attention to the difference between the writing method of the query statement and the common index .
14、 To avoid the where The clause performs an expression operation on the field
such as :
select user_id,user_project from user_base where age*2=36;
In the field on the line of arithmetic operations , This will cause the engine to give up using indexes , It is suggested to change it to :
select user_id,user_project from user_base where age=36/2;
15、 Avoid implicit type conversion
where Clause column Type conversion when the type of the field is inconsistent with the type of the parameter passed in , It is suggested that where Parameter types in .

16、 For federated indexes , Follow the leftmost prefix rule
For example, an index contains fields id、name、school, It can be used directly id Field , It's fine too id、name In this order , however name;school Can't use this index . So when you create a union index, you must pay attention to the order of index fields , Common query fields are placed at the top .
17、 You can use when necessary force index To force the query to go to an index
sometimes MySQL The optimizer takes the index it thinks appropriate to retrieve SQL sentence , But maybe the index it uses is not what we want . At this point, it can be used forceindex To force the optimizer to use the index that we set .
18、 Note the range query statement
For federated indexes , If there are scope queries , such as between、>、< Under equal conditions , It will invalidate the following index fields .
19、 About JOIN Optimize

LEFT JOIN A The table is the driving table ,INNER JOIN MySQL It will automatically find the table with less data to drive the table ,RIGHT JOIN B The table is the driving table .
Be careful :
1)MySQL There is no full join, It can be solved in the following ways :
select * from A left join B on B.name = A.namewhere B.name is nullunion allselect * from B;
2) Use as much as possible inner join, avoid left join:
The tables participating in the union query are at least 2 A watch , In general, there are different sizes . If the connection is inner join, In the absence of other filter conditions MySQL Will automatically select the small table as the driving table , however left join In the selection of driving table, we follow the principle of left driving right , namely left join The table on the left is called the drive table .
3) Make good use of indexes :
The index field of the driven table is used as on Restricted fields for .
4) Use a small watch to drive a large watch :

It can be seen intuitively from the schematic diagram that if the drive table can be reduced , Reduce the number of loops in nested loops , In order to reduce IO Total amount and CPU The number of operations .
5) Skillfully use STRAIGHT_JOIN:
inner join By MySQL Select driver table , But in some special cases, you need to choose another table as the driving table , Such as the group by、order by etc. 「Using filesort」、「Using temporary」 when .STRAIGHT_JOIN To force the connection order , stay STRAIGHT_JOIN The table name on the left is the drive table , On the right is the driven table . In the use of STRAIGHT_JOIN There is a precondition that the query is an inner join , That is to say inner join. Other links are not recommended STRAIGHT_JOIN, Otherwise, the query result may be inaccurate .

This approach can sometimes reduce 3 Times of time .
above 19 strip MySQL I hope the optimization method can help you !
Write it at the end
You can leave a message below to discuss what you don't understand , You can also ask me by private message. I will reply after seeing it . Finally, if the article is helpful to you, please remember to give me a like , Pay attention and don't get lost
@ Terminal R & D department
Fresh dry goods are shared every day !
reply 【idea Activate 】 You can get idea How to activate
reply 【Java】 obtain java Relevant video tutorials and materials
reply 【SpringCloud】 obtain SpringCloud Many relevant learning materials
reply 【python】 Get the full set 0 Basics Python Knowledge Manual
reply 【2020】 obtain 2020java Related interview questions tutorial
reply 【 Add group 】 You can join the technical exchange group related to the terminal R & D department
Read more
use Spring Of BeanUtils front , I suggest you understand these pits first !
lazy-mock , A lazy tool for generating backend simulation data
In Huawei Hongmeng OS Try some fresh food , My first one “hello world”, take off !
The byte is bouncing :i++ Is it thread safe ?
One SQL Accidents caused by , Colleagues are fired directly !!
Too much ! Check Alibaba cloud ECS Of CPU Incredibly reach 100%
a vue Write powerful swagger-ui, A little show ( Open source address attached )
Believe in yourself , Nothing is impossible , Only unexpected, not only technology is obtained here !
If you like, just give me “ Looking at ”边栏推荐
- 什么是免疫组织化学实验? 免疫组织化学实验
- STM32------ADC(电压检测)
- Flutter中的GetX状态管理用起来真的那么香吗?
- The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
- MySQL事務隔離
- Bitmap加载内存分析
- 《阿里云天池大赛赛题解析》——O2O优惠卷预测
- Summary of some indicators for evaluating and selecting the best learning model
- Some explanations of Tim timer of embedded interface and STM32 template library function of NVIC
- PHP的curl功能扩展基本用法
猜你喜欢

【HackTheBox】Fawn

图像分割-数据标注

生鲜前置仓的面子和里子

企业网站的制作流程是什么?设计和制作一个网站需要多长时间?

C# Winform 自定义进度条ProgressBar

Data interpretation! Ideal L9 sprints to "sell more than 10000 yuan a month" to grab share from BBA

What is an immunohistochemical experiment? Immunohistochemical experiment

Cause analysis and Countermeasures for FANUC robot srvo-050 collision detection alarm (available for personal test)
Detailed usage of exists in SQL statements

WebService客户端请求失败 can not create a secure xmlinputfactory
随机推荐
STM32-------外部中断
C # read the occupied size of memory module and hard disk
3D打印和激光切割流程的初步了解
C WinForm custom progress bar ProgressBar
谈谈数字化转型晓知识
[observation] Dell technology + Intel aoteng Technology: leading storage innovation with "nanosecond speed"
Cause analysis and Countermeasures for FANUC robot srvo-050 collision detection alarm (available for personal test)
STM32 ------ external interrupt
Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
短视频挺进在线音乐腹地
HDLBits-&gt;Circuits-&gt;Arithmetic Circuitd-&gt;3-bit binary adder
CTF go topic recurrence
Kotlin coroutine asynchronous flow
Activity的onSaveInstanceState回调时机
Install using snap in opencloudos NET 6
MySQL索引底层为什么用B+树?看完这篇文章,轻松应对面试。
Tencent lightweight + pagoda building document online preview project kkfileview
详解四元数
堡垒机安装pytorch,mmcv,mmclassification,并训练自己的数据集
国家邮政局等三部门:加强涉邮政快递个人信息安全治理,推行隐私面单、虚拟号码等个人信息去标识化技术
