当前位置:网站首页>19 MySQL database optimization methods
19 MySQL database optimization methods
2022-06-13 03:37:00 【Chaos scar】
So in order to cope with the increasing traffic , We need to be right about mysql Make corresponding countermeasures , Further optimization mysql To achieve the desired results .
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 .
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 Optimization methods hope to help you oh ~
边栏推荐
- MASA Auth - 从用户的角度看整体设计
- look on? What is the case between neo4j and ongdb?
- Doris data import broker load
- Polymorphism in golang
- How to Draw Useful Technical Architecture Diagrams
- Spark kernel (execution principle) environment preparation /spark job submission process
- C language programming -- input a string (including letters, numbers, punctuation marks, and space characters) from the keyboard, calculate the actual number of characters and print out, that is, it d
- Cross border M & a database: SDC cross border database, Thomson database, A-share listed company M & a database and other multi index data (4w+)
- LVS four layer load balancing cluster (6) LVS working mode
- Solve the error in CONDA installation PyMOL
猜你喜欢
DTCC | 2021 China map database technology conference link sharing
Spark optimization - Performance (general performance, operator, shuffle, JVM) tuning
[azure data platform] ETL tool (8) - ADF dataset and link service
Multi thread writing method and the making of slot machine
MySQL learning summary Xi: detailed explanation of the use of stored procedures and stored functions
Use cypher to get the tree of the specified structure
The latest collation of the number of years of education per capita in the country and provinces -1989-2020- includes the annual original data, calculation process and result summary
Nuggets new oil: financial knowledge map data modeling and actual sharing
look on? What is the case between neo4j and ongdb?
Summary of rust language practice
随机推荐
Complete set of Stata code commands: follow and verify do files, common Stata commands, code collection, panel entropy method
2000-2019 enterprise registration data of all provinces, cities and counties in China (including longitude and latitude, registration number and other multi indicator information)
MySQL learning summary 12: system variables, user variables, definition conditions and handlers
Doris creates OLAP, mysql, and broker tables
MySQL auto sort function deny_ rank() over()、rank() over()、row_ Num() over() usage and differences
ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
Azure SQL db/dw series (10) -- re understanding the query store (3) -- configuring the query store
Masa auth - SSO and identity design
Common command records of redis client
在JDBC连接数据库时报错:Connection to 139.9.130.37:15400 refused.
Batch image Download & socket dialogue
China Civil Aviation Statistical Yearbook (1996-2020)
Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
Union, intersection and difference sets of different MySQL databases
Coal industry database - coal price, consumption, power generation & Provincial Civil and industrial power consumption data
LVS four layer load balancing cluster (6) LVS working mode
(9) Explain broadcasting mechanism in detail
Azure SQL db/dw series (14) -- using query store (3) -- common scenarios
PHP import classes in namespace
LVS四層負載均衡集群(5)LVS概述