当前位置:网站首页>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 ”边栏推荐
- 生鲜前置仓的面子和里子
- Zynq ultrascale+ RF data converter IP configuration - ADC
- 根据先序遍历和中序遍历生成后序遍历
- Summary of cloud native pipeline tools
- The fortress machine installs pytorch, mmcv, and mmclassification, and trains its own data sets
- FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策(亲测可用)
- TDD development mode process recommendation
- Desai wisdom number - histogram (basic histogram): the way to celebrate father's day in 2022
- Telecommuting: how to become a master of time management| Community essay solicitation
- Construction of cache stack FIFO in different application scenarios for PLC data operation series (detailed algorithm explanation)
猜你喜欢

什么是免疫组织化学实验? 免疫组织化学实验

浩哥的博客之路

Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
云原生流水线工具汇总

Is the geTx status management in the flutter really so good to use?

Ambire Guide: the arbitrum odyssey begins! Week 1 - Cross Chain Bridge

嵌入式接口之TIM定时器与NVIC的STM32模板库函数的一些解释

Cs5213 HDMI to VGA with audio signal output scheme

腾讯会议号设计的几种猜测

图论(树的直径)
随机推荐
Generate post order traversal according to pre order traversal and mid order traversal
Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
百万消息量IM系统技术要点分享
光大期货安全吗?开户需要什么东西?
企业网站的制作流程是什么?设计和制作一个网站需要多长时间?
The Sandbox 与 BAYZ 达成合作,共同带动巴西的元宇宙发展
国内外最好的12款项目管理系统优劣势分析
Detailed process of deploying redis cluster and micro service project in docker
The sandbox week is coming!
Fabric. JS manual bold text iText
Sorry, your USB cable may be wrong!
保障特殊困难群体安全,广州民政全力做好三防工作
根据先序遍历和中序遍历生成后序遍历
AndroidKotlin全面详细类使用语法学习指南
嵌入式接口之TIM定时器与NVIC的STM32模板库函数的一些解释
The national post office and other three departments: strengthen the security management of personal information related to postal express delivery, and promote the de identification technology of per
Stm32 - - - - interruption externe
AIX system monthly maintenance check (I)
What is an applet container
【Xilinx AX7103 MicroBalze学习笔记6】MicroBlaze 自定义 IP 核封装实验
