当前位置:网站首页>Explanation of index failure principle and its common situations
Explanation of index failure principle and its common situations
2022-07-27 04:56:00 【Meme_ xp】
Let's first look at the single valued index b+ Tree image

Each node stores only one key value pair
Let's take a look at the structure of the joint index

Yes (2,4) Conduct joint indexing
We are right. a The index value sort is :1,1,2,2,3,3
Yes b Sort :1,2,1,4,1,2
You can see a The fields are ordered ,b Fields are out of order ( because B+ The tree can only select one field to build an ordered tree )
We can see that a Is ordered , stay a Under the premise of confirmation, our b It's also ordered
Best left prefix principle
select * from testTable where a=1 and b=2
a Field in B+ There's order in the trees , So we can locate... By binary search a=1 The location of .
Secondly, in a In certain cases ,b It's relatively orderly , Because of order , So we can also find it by binary search b=2 The location of .
Take a look at the special case
select * from testTable where b=2
b The premise of order is in a In certain cases , Now? a It's gone , that b There must be no order , In a disordered B+ It's impossible to locate... In a tree by binary search b Field . So the index is invalid
Range queries Right failure principle

select * from testTable where a>1 and b=2
First a Field in B+ There's order in the trees , So we can use binary search to locate 1, And then put all the greater than 1 We'll take out the data ,a You can use indexes .
however b The premise of order is a It's a certain value , So now a The value of is greater than 1 Of , There may be 10 More than 1 Of a, There may be a hundred a.
like Index invalidation principle
The principle is almost the same as the above , Let's take an example
where name like "a%"
where name like "%a%"
where name like "%a"
% Use of
1.% Put it on the right side. , On behalf of the inquiry to "a" Initial data , Such as :abc
2. Two %%, Represents that the query data contains "a" The data of , Such as :cab、cba、abc
3.% Put it on the left side. , On behalf of the inquiry to "a" For the ending data , Such as cba
Why? % On the right, you can sometimes use indexes
1.% On the right, it's called : Prefix
2.%% be called : Infix
3.% On the left is called : suffix
Here is still the best left prefix :

How strings are sorted : First, sort by the first letter , If the first letter is the same , Just sort by the second letter ... And so on
So there are three situations
One 、% Number one on the right ( Prefix )
because B+ The index order of the tree , It's sorted by the size of the first letter , Prefix matching is also matching initials . So it can be B+ An orderly search on the tree , Find the data whose initials match the requirements . So sometimes you can use indexes .
Two 、% Number one on the left
Is the data that matches the end of the string , We said sort rules above , The letters at the end are out of order , So you can't query in index order , You don't need an index .
3、 ... and 、 Two %% Number
This is to query the letters in any position to meet the conditions , Only the initials are indexed , The letters in other places are relatively unordered , So there's no index to find letters anywhere .
Index failure condition portal
In summary :
1. Implicit type conversion is used
2. The expression used by the index calculation
select * from user where age - 2 =18;//1
select * from user where age = 18+2 ;//2
In the first case, the index will be used , The second is not
3. A function expression is used
select * from user where left(phone,3)='133';//1
select * from user where phone like='133%';//2
In the first case, the index will be used , The second is not
The above three situations are summarized
In fact, the use of index depends on B-tree Traversal of index tree , The traversal of the index tree depends on the ordering of the underlying leaf nodes , When the indexed field performs the above three operations , It is possible that the new arrangement order of this field is different from the original arrangement order in the leaf node layer of the index tree , It destroys the order of leaf nodes ,sql Statement execution time ,mysql The executor of cannot judge whether the original index tree can be retrieved . Led to sql The statement executor does not use the index . Our intuitive feeling is that the index is invalid
4.% Fuzzy matching , There is analysis on it .
5. The used index field is not the leftmost field of the union index , See above for analysis
A small summary :4,5 The situation is the above analysis , Follow the leftmost matching principle
If index coverage occurs in these five cases , That is, when there is no need to return the table , The index tree will still be used
边栏推荐
- Basic configuration of static routing to achieve network wide accessibility
- Shell programming enhancements
- What is the difference between using varchar type and using date type for timestamp column?
- Dynamic routing configuration
- Yolov4 network details
- Structural mode - decorator mode
- 使用Unity做一个艺术字系统
- Digital integrated circuit: MOS tube device chapter (II)
- HCIA static routing basic simulation experiment
- 如何做数据平滑迁移:双写方案
猜你喜欢

Final Cut Pro中文教程 (2) 素材窗口的认识

C语言 通讯录管理系统(链表,手机号码分段存储,txt文件存取,完整源码)

博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”

Huawei's entry into the commercial market: due to the trend, there are many challenges

The price reduction of iphone13 is just a show. Consumers are waiting for iphone14

Replication of df-gan experiment -- detailed steps of replication of dfgan and forwarding from remote port to local port using mobaxtem

Digital integrated circuit: CMOS inverter (I) static characteristics

Photoshop裁剪工具隐藏技巧

缓存读写策略:CacheAside、Read/WriteThrough及WriteBack策略

STL upper series - detailed explanation of list container
随机推荐
Session&Cookie&token
Visualization domain svg
Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
报错:cannot read poperties of undefined(reading ‘then‘)
「Photoshop2021入门教程」对齐与分布制作波点图案
使用Unity做一个艺术字系统
Summary of fire safety training materials
On the problem that Gorm's beforedelete hook method does not work
数组中的最大值,最小值,冒泡排序
Shift right of negative numbers
日落红暖色调调色滤镜luts预设Sunset LUTs 1
C language - two dimensional array, pointer
2019强网杯upload
Final Cut Pro中文教程 (1) 基础认识Final Cut Pro
在有序数组找具体某个数字
Dry goods | how can independent station operation improve online chat customer service?
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
Solution: read the files with different names in the two folders and deal with the files with different mappings
TCP's three handshakes and four waves
Structural mode - facade mode