当前位置:网站首页>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/
边栏推荐
- 你的公司会选择开发数据中台吗?
- SQL Server row to column (pivot), column to row (unpivot)
- C language -- 10 first knowledge of structure
- php伪协议实现命令执行详情
- How to view the occupied space of a table in MySQL database
- Redis cache avalanche
- Rotate menu 2.0
- 登堂入室之soc开发环境及硬件开发准备
- Self made table
- [qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?
猜你喜欢

C language -- 1 c language cognition

Before we learn about high-performance computing, let's take a look at its history

【MySQL】表数据的增删查改(DML)

详解MATLAB中与矩阵运算有关的算术运算符(加、减、乘、除、点乘、点除、乘方)

设计多层PCB板需要注意哪些事项?

Abbexa 8-OHdG CLIA kit solution

Install MySQL on Linux system. Problems encountered in xshell
php伪协议实现命令执行详情

北大青鸟昌平校区:高中学历可以学UI吗?
![[nk] Niuke monthly competition 51 f-average question](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] Niuke monthly competition 51 f-average question
随机推荐
Interview Essentials - basic knowledge of synchronized underlying principles
SQL Server2019安装的详细步骤实战记录(亲测可用)
Before we learn about high-performance computing, let's take a look at its history
防抖和节流
Understanding of related concepts of target detection
ThinkPHP v6.0.x反序列化漏洞复现
Anti shake and throttling
C language ---5 initial string, escape character and comment
Abbexa AML1 DNA binding ELISA Kit instructions
C language -- 7 operators
C language -- 1 c language cognition
用少儿编程思维塑造青少年领悟能力
数据库系统概论 ---- 第一章 -- 绪论(重要知识点)
php伪协议实现命令执行详情
标配双安全气囊,价格屠夫长安Lumin 4.89万起售
Leetcode advanced road - 169 Most elements
SQL第四练:字符串处理函数
2022 - 06 - 09 rk817 PMU Battery Temperature Detection
构建幼儿Steam教育实施策略
C language -- 3 variables for beginners