当前位置:网站首页>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 .
边栏推荐
- Research on next generation distributed file system
- 元数据管理,数字化时代企业的基础建设
- 【QNX Hypervisor 2.2 用户手册】3.2.3 ACPI表和FDT
- C language -- 14 loop statement for
- 华为云鲲鹏DevKit代码迁移实战
- [CEPH] CEPH configuration source code analysis | common/config*
- Aesthetic experience (episode 237) Luo Guozheng
- How to locate the hot problem of the game
- Huawei cloud Kunpeng devkit code migration practice
- Developers changing the world - Yao Guang teenagers playing Tetris
猜你喜欢

当前有哪些主流的全光技术方案?-下篇

How can bi help enterprises reduce labor, time and management costs?

C language -- 12 branch statement switch

锐捷x32pro刷openwrt开启无线160MHz

ETL的使用过程中遇到的坑(ETL中文乱码)

数据处理时代,数据分析成为基础建设

The value of Bi in the enterprise: business analysis and development decision

Stream流的常用方法-Lambder

AFL fuzzy multithreading

自定义类型:结构体
随机推荐
[QNX hypervisor 2.2 user manual] 3.2.2 VM configuration example
[代码]神经符号生成机器
Adding rendererdata of URP with script
Uniapp native JS to convert the Gregorian calendar to the lunar calendar
三部曲解下棋先手后手问题
用基础比率重写清晰的贝叶斯公式
ETL的使用过程中遇到的坑(ETL中文乱码)
Form form of the uniapp uview framework, input the verification mobile number and verification micro signal
Set up an online help center to easily help customers solve problems
ADS,AXD基本使用说明
台积电刘德音:不担心半导体库存修正及美日韩合作,今年业绩将增长30%!
单调栈结构
套路解---马走棋盘问题
In the introductory study of data visualization, we should be alert to pitfalls and misunderstandings and grasp key nodes
JS Touch
c语言学习回顾---1 基础知识回顾
实时商业智能BI(二):合理的ETL架构设计实现准实时商业智能BI
c语言---8 初识常见关键字
NaturalSpeech模型合成语音在CMOS测试中首次达到真人语音水平
XML & XPath parsing