当前位置:网站首页>MySQL index invalidation scenario

MySQL index invalidation scenario

2022-06-10 18:38:00 -Bruce Lee

1. Full value match index

If there is an index index_id The corresponding index field is id, And index index_id_name The corresponding index field is id and name, In this case, we use id and name, Such as where id=1 and name=‘zs’ when , Indexes index_id invalid . That is, when there are multiple fields in the condition , Give priority to the composite index in the matching condition .

2. Left most matching principle

MySQL Up to... Can be created in 16 An index , When using composite indexes , Note that index matching starts with the leftmost index field , If you skip a field , The following index fields are all invalid .
Like a table There is only one index and it is a composite index , The fields are sorted as id,name,age,address

Serial number Conditions Whether to use the index Index matching fields
1where id = 1 yes id
2where id = 1 and age= 26 yes id,age
3where id = 1 and age= 26 and name = ‘zs’ yes id,name, age
4where id = 1 and age= 26 yes id
5where id = 1 and age= 26 and name = ‘zs’ and address = ‘sh’ yes id,name,age,address
6where id = 1 and name = ‘zs’ and address = ‘sh’ yes id,name
7where age= 26 no
8where name = ‘zs’ and address = ‘sh’ no

Be careful 1,2,3,5 The index condition fields of the condition of all match , Because the conditional fields appearing in the conditional statements are from left to right in the matching composite index .
Look again 4,6 The condition of is also indexed , but 4 In the field age No match , because age To the left of the field name The field did not result in age Field skipped , Also in 6 in address Also because the field on the left age Did not cause to be skipped .
stay 7,8 In the condition of , The left most field id non-existent , So all the following fields are ignored , Cause index not to be used .

The matching reason here , Need to know MySQL Index the underlying data structure , For further research, please refer to the article InnoDB Deduction of index in

4. Primary key insertion order

With InnoDB The storage engine, for example , All data is stored based on the primary key , The primary key is stored in the data page in descending order , If the primary key value of the inserted table is not incremental , The data page itself has a size , Store in one data page 100 Take data for example , When the primary key value is added non incrementally , It will cause the data to move in each data page because of sorting , That leads to a lot of fragmentation , This is very performance consuming , It is recommended that the primary key be stored in auto increment mode .

5. Calculation 、 function 、 Type conversion

When the condition is used in the calculation, such as adding , Minus and so on where id+1=1000, Or use functions such as where SUBSTRING(name)=‘zs’, Or type conversion such as id by int type , Conditional use string where id = ‘100’ These conditions can lead to index invalidation .

6. The column index to the right of the range condition fails

Like a table There is only one index and it is a composite index , The fields are sorted as id,name,age,address
The query conditions are where id=1 and name=‘zs’ and age>20 and address=‘sh’ At this time, the above composite index will be used to , But the only index fields that match are id,name,age These three ,address because age Judge the condition for the range so that the index cannot be matched .
If the query condition is where id>1 and name=‘zs’ and age=20 and address=‘sh’ At this time, only id This field can match the index , The rest are id The index fields on the right will be invalidated .
In this case , It is recommended to put the range index field at the end of the composite index .

7. It's not equal to (!= or <>) Index failure

Such as where id !=1 or where id <> 1 This situation id Cannot match to index .

8. is null You can use index ,is not null Index not available ,not like Index not available

In this case , It is recommended to set the field to... When designing the data table NOT NULL constraint , Such as INT The type field , The default value can be set to 0.

9. like Use wildcards % Invalid start index

As a condition 1:where name like ‘%zs%’
Conditions 2:where name like ‘zs%’
result : Conditions 1 Index not available , Conditions 2 You can use index

10. or There are non indexed columns before and after , Index failure

If a table has only one index index_age, The query conditions are where age = 20 or name = ‘zs’, The index cannot be matched at this time . If you are targeting name Field to build an index index_name, This query condition can be matched to the index .

11. Database and table character sets are used uniformly utf8mb4

Unified use uft8mb4 Good compatibility , The unified character set can avoid the chaos caused by character set conversion , It can also avoid index invalidation caused by conversion before comparing different character sets .


problem

  1. The fields in the composite index are sorted, such as , Indexes id,name,age,address , Do we have to sort according to this when using ?
    In practice , The field sequence is OK ,SQL It will be optimized by the optimizer after execution .
原网站

版权声明
本文为[-Bruce Lee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101800151607.html