当前位置:网站首页>Index optimization principle

Index optimization principle

2022-06-12 19:50:00 WD Technology

1、 Full match
Try to use full value matching in query statements .
 Insert picture description here
2、 The left prefix principle
If an index is a composite index , Indexed multiple columns , Follow the left prefix rule , That is, the query starts from the left prefix of the index , You cannot skip the middle column of the index .
 Insert picture description here
 Insert picture description here
3、 Do not operate on index columns
Operations include : Calculation 、 function 、 Automatic or manual type conversion , Do not do any of the above on the index column , Otherwise, the index will be invalidated and the full table scan will be performed
 Insert picture description here
 Insert picture description here
4、 The column index on the right of the range condition is invalid
When the range condition appears in the composite index , Then all column indexes on the right of the range condition are invalid . So when designing and using indexes , The column that frequently performs range queries should be taken as the rightmost column of the index .
 Insert picture description here
**5、 On demand data , To use less select ***
Use less in query select *, otherwise MySQL You need to traverse which fields exist in the result column . meanwhile , Overlay indexes should be used whenever possible , It can avoid looking up the table twice
 Insert picture description here
6、 Avoid using != or <>
In use is not equal to (!= or <>) when , Index not available , Causes a full table scan .
 Insert picture description here
7、 avoid is null or is not null
The index of the query condition that determines the null value is invalid , Causes a full table scan .
 Insert picture description here
8、 avoid like Start with a wildcard
Query criteria like Use wildcards % start , Such as name like ("%abc"), Index failure , Causes a full table scan .
 Insert picture description here
If the business needs to use wildcards % What to do at the beginning ?
The answer is : Use overlay index
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
9、 To use less or
Use or To connect query criteria , Index failure .
 Insert picture description here
10、 Expand the index as much as possible , Don't create a new index . Let's say it's already in the table a The index of , Now want to add (a,b) The index of , All you need to do is change the original index , Building unnecessary indexes will increase MySQL Space

11、 If you decide how many pieces of data there are , Use limit Limit the ,MySQL When the corresponding number of pieces of data is found , Will stop searching

12、join grammar , Try to put a small watch in front , The need to on Field on , Data types are consistent , And set the corresponding index , otherwise MySQL Cannot use an index to join Inquire about

13、= and in You can order , such as a = 1 and b = 2 and c = 3 establish (a,b,c) Indexes can be in any order ,mysql The query optimizer will help you optimize it into a form that the index can recognize

原网站

版权声明
本文为[WD Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281534382608.html