当前位置:网站首页>19 MySQL optimizations commonly used in projects
19 MySQL optimizations commonly used in projects
2022-06-10 22:07:00 【dotnet9.com】
Before writing the article , First, thank you. Feiyou Technology Documents provided by Mr. Lu ..
Make a statement : The following optimization schemes are all based on “ Mysql- Indexes -BTree type ” Of
One 、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 . Note that the common unfriendly values are :Using filesort, Using temporary
Two 、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 table_name where num in(1,2,3) For continuous values , It works between Don't use in 了 ; Or use connections to replace .
3、 ... and 、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 , The front end also needs to be updated . So the request is directly in select Followed by the field name .
Four 、 When only one piece of data is needed , Use limit 1
This is to make EXPLAIN in type Column reach const type
5、 ... and 、 If the sort field does not use an index , Just sort as little as possible
6、 ... and 、 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、 ... and 、 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、 ... and 、 Don't use ORDER BY RAND()
select id from `table_name` order by rand() limit 1000;
above sql sentence , It can be optimized to
select id from `table_name` t1 join (select rand() * (select max(id) from `table_name`) as nid) t2 on t1.id > t2.nid limit 1000;
Nine 、 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
Ten 、 Use reasonable paging methods to improve the efficiency of paging
select id,name from table_name 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 table_name where id> 866612 limit 20
11、 ... and 、 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
Twelve 、 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、 ... and 、 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 Results not used
So how to solve this problem , answer : Use full text indexing
We often use select id,fnum,fdst from table_name 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 `table_name` ADD FULLTEXT INDEX `idx_user_name` (`user_name`);
Using full-text indexing sql The sentence is :
select id,fnum,fdst from table_name 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
fourteen 、 To avoid the where The clause performs an expression operation on the field
such as
select user_id,user_project from table_name 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 table_name where age=36/2;
15、 ... and 、 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
sixteen 、 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
seventeen 、 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 force index To force the optimizer to use the index that we set .
eighteen 、 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 .
nineteen 、 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 :MySQL There is no full join, It can be solved in the following ways
select * from A left join B on B.name = A.name
where B.name is null
union all
select * from B;
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 .
Make good use of indexes
The index field of the driven table is used as on Restricted fields for .
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 .
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 .
Only the above optimization schemes are listed here , Of course, there are other ways to optimize , You can try to find out , Thank you for attention ..
Wrote last , Welcome to your attention :http://fenxianglu.cn/
边栏推荐
- 旋转菜单3.0
- 旋转菜单2.0
- 目标检测相关概念的理解
- Super detailed tutorial for installing mysql8 in centos7 (no pit!)
- Quick start to VISSIM simulation
- SQL Server2019安装的详细步骤实战记录(亲测可用)
- CentOS7环境下MySQL8常用命令小结
- Detailed explanation of Lora module wireless transceiver communication technology
- C language -- 4 first-time constant
- Are you still writing the TS type code
猜你喜欢

Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)

Understanding deep learning attention
![[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas](/img/00/798455f2db289e3aaed36981ea1e7c.png)
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas

What should be paid attention to when designing Multilayer PCB?

Qingniao Changping campus of Peking University: can I learn UI with a high school degree?

Notes to entry: do I need to know programming for O & M?

JS mobile terminal copy text to clipboard code

SQL第四练:字符串处理函数
An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)

C language -- 4 first-time constant
随机推荐
Junior high school graduates who choose secondary vocational schools can also be promoted to institutions of higher learning
Anti shake and throttling
Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)
Constructing the implementation strategy of steam education for children
2021年平均工资出炉,IT行业不出所料
Redis cluster configuration
H. Relationship among Nalu, RBSP and sodb in 264
NFT版权/版税
C language learning review -- 1 basic knowledge review
[qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)
Realize OSD reverse color on YUV image according to background color
Before we learn about high-performance computing, let's take a look at its history
目标检测相关概念的理解
标配双安全气囊,价格屠夫长安Lumin 4.89万起售
Tableau auto - fabriqué
SQL Server row to column (pivot), column to row (unpivot)
C language -- 4 first-time constant
Only this is the most true reason why leaders promote you. The rest is nonsense!
Abbexa cell free DNA kit instructions