当前位置:网站首页>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 |
|---|---|---|---|
| 1 | where id = 1 | yes | id |
| 2 | where id = 1 and age= 26 | yes | id,age |
| 3 | where id = 1 and age= 26 and name = ‘zs’ | yes | id,name, age |
| 4 | where id = 1 and age= 26 | yes | id |
| 5 | where id = 1 and age= 26 and name = ‘zs’ and address = ‘sh’ | yes | id,name,age,address |
| 6 | where id = 1 and name = ‘zs’ and address = ‘sh’ | yes | id,name |
| 7 | where age= 26 | no | |
| 8 | where 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
- 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 .
边栏推荐
猜你喜欢

改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年

如何正确理解商业智能BI的实时性?

光储直柔配电系统浅析

Developers changing the world - Yao Guang teenagers playing Tetris

After the qtmqtt source code compilation is set to keepalive, the Ping package timeout error does not return a problem repair (qmqtt:: mqttnopingresponse, qmqtt:: clientprivate:: onpingtimeo)

How to realize the management "desire" of Business Intelligence BI service objects and enterprise managers?

In the digital era, how can enterprises manage data security and ensure the security of data assets

VMware ESXi 各版本号对照表

三部曲解下棋先手后手问题

leecode27,977-双指针法
随机推荐
【QNX Hypervisor 2.2 用户手册】3.2.2 VM配置示例
数字化时代,企业如何进行数据安全治理,保障数据资产安全
(CVPR 2020) RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds
Data URL
AGI基础,不确定性推理,主观逻辑一书ppt1
Qtablewidget / qtableview practical tips
IBOX系统开发核心功能和部分核心源码
C language -- 14 loop statement for
Building smart community Internet of things based on smart road lamp posts
How to realize the management "desire" of Business Intelligence BI service objects and enterprise managers?
TSMC liudeyin: do not worry about semiconductor inventory correction and cooperation between the United States, Japan and South Korea. This year's performance will increase by 30%!
跨域报错:When allowCredentials is true, allowedOrigins cannot contain the special value “*“
微信小程序仿陶票票课程设计
Metadata management, the basic construction of enterprises in the digital era
商业智能BI在企业的价值之:业务分析发展决策
Stream流的常用方法-Lambder
[Interface tutorial] how does easycvr set platform cascading through the interface?
How to locate the hot problem of the game
uniapp uview 框架的form表单,输入校验手机号、校验微信号
华为云HCDE上云之路第二期:华为云如何助力制造业中小企业数字化转型?