当前位置:网站首页>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 .
边栏推荐
- CodeCraft-22 and Codeforces Round #795 (Div. 2)
- 微信小程序,获取当前页面,判断当前页面是不是tabbar页面
- 【QNX Hypervisor 2.2 用户手册】3.2.1 VM配置语法
- This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
- 企业管理者的质疑,这么多年的信息化,我们的钱花哪去了?
- Metadata management, the basic construction of enterprises in the digital era
- VMware esxi version number comparison table
- 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)
- mysql备份和shell脚本手动执行没问题,crontab定时执行失败
- C language -- 14 loop statement for
猜你喜欢

Adding rendererdata of URP with script

In the introductory study of data visualization, we should be alert to pitfalls and misunderstandings and grasp key nodes

二叉树之Morris遍历

The question of enterprise managers, where have we spent our money after so many years of informatization?

c语言---7 初识操作符

Set up an online help center to easily help customers solve problems

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

C语言---1 C语言认知

两部门发文明确校外培训机构消防安全条件

How to realize the management "desire" of Business Intelligence BI service objects and enterprise managers?
随机推荐
实时商业智能BI(二):合理的ETL架构设计实现准实时商业智能BI
干货 | 一文搞定 uiautomator2 自动化测试工具使用
Step on the pit. The BigDecimal was improperly used, resulting in P0 accident!
Developers changing the world - Yao Guang teenagers playing Tetris
c语言---2 初识数据类型
Aesthetic experience (episode 237) Luo Guozheng
套路解---马走棋盘问题
Data URL
[CEPH] CEPH configuration source code analysis | common/config*
[QNX hypervisor 2.2 user manual] 3.2.2 VM configuration example
Semiconductor silicon continued to fall short of demand, and Shenggao's long-term contract price rose by 30%!
改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
C language -- 14 loop statement for
[QNX hypervisor 2.2 user manual] 3.3 configure guest
leecode27,977-双指针法
What is Bi? Talk about the definition and function of Bi
换根呀呀啊呀
单调栈结构
c语言学习回顾---1 基础知识回顾
三部曲套路解bob活命问题