当前位置:网站首页>Two minutes, talk about some wrong understandings of MySQL index
Two minutes, talk about some wrong understandings of MySQL index
2022-07-07 22:52:00 【Yes' level training strategy】
Hello , I am a yes.
Today, let's talk about MySQL InnoDB Some easily misunderstood points of the index .
Don't talk much , Direct departure !
You may not use the index you think you will use
In order that the story can develop smoothly , Let's start with a watch , DDL as follows :
Now I want to execute this statement SELECT * FROM yes.t1 where f3 = 11;
Which index do you think is used ?
Many students may feel that although there is idx_f2_f3 This joint index , But the query criteria are f3, No, f2, Does not conform to the leftmost matching principle , Therefore, it is obvious that only full table scanning can be performed .
Let's come. explain once , have a look :
You can see the result ,key yes idx_f2_f3 , So this query uses idx_f2_f3 This index !
Someone here may not understand , How can joint indexing be used if it does not conform to the leftmost indexing principle ?
from explain As a result , Do use the union index , But it's type yes index, therefore It's not the kind of index you think , It is equal to the full scan of the joint index to obtain the results .
It's ok if you don't understand now , Listen to the following explanation .
What exactly is using an index ?
We all know InnoDB It is based on clustering index to build data , Without creating other secondary indexes , You must and can only use cluster index to query data , Because the data is only on this index , Then you don't need this index to find data. What do you use ?
If you build other secondary indexes , Then you may not need cluster index to find data , Instead, only the upper secondary index can directly get the desired data ( This is the example of the above query ).
in summary , you are here InnoDB Find any data , Can only use index .
Then why do some people say that they don't follow the index on weekdays , After walking, the whole table is scanned ?
We all know the default InnoDB The index structure of is B+ Trees , This tree has a feature , Its leaf nodes are connected by linked lists .
So as long as we find primary key The leftmost leaf node , Then traverse the scan in right order , You can get all the records of this form , This scanning method is called full table scanning .
Tips: : I don't know MySQL Is the location of the leftmost leaf node recorded or the leftmost leaf node found according to the root node , Anyway, it means that , It does not affect our understanding of full table scanning .
Then what is index ?
Look at the red query route , It can use the fast search ability of the tree to locate the page where the data is located , Such a query can count as an index .
Then push back from the primary key index to the secondary index , It's the same thing . It can use the characteristics of secondary index tree to find data , Is to use the index .
But if you can't use the fast search of the secondary index tree , Is there such a thing as full table scanning , The operation of directly scanning all leaf nodes of the secondary index ?
yes , we have , This is our example above , I'll move down the screenshot of the execution result :
This query MySQL Full scan secondary index idx_f2_f3 To directly return the required data .
The reason is that the secondary index already contains all the data required by the query :f1、f2、f3, So you can scan the secondary index directly .
You can see explain Of type=index
, That is, full scan secondary index .
This is also called using the index .
At this time, we use f2 As a query condition , This conforms to the leftmost matching principle , Let's see explain What's the difference? :
You can see that it is still used idx_f2_f3 , It's just type become ref , This ref It shows that you can quickly query the results when matching with constants .
This is also called using the index .
Okay , Let's sum up what it means to use an index :
- Use the primary key index to quickly find
- Fast search with secondary index
- Full scan the secondary index to find
The above three kinds are what we usually call using indexes , Then the full scan of the primary key index is what we call the full table scan ( Equivalent to not using an index , But we know that it actually uses the primary key index, right ?).
thus , I think you should already know , What exactly is using an index .
Little doubt
About MySQL Some understanding of the index is over , But look at the above example , Some students may be a little confused :
Since they all need full scanning , Why scan the secondary index , Instead of the primary key index ?
Because the primary key index stores all the data , Including transactions related to data rows ID、 Rollback pointer, etc .
The secondary index does not contain these , It only has primary key value and index column value .
Therefore, the overhead of directly reading the secondary index will be smaller , therefore When the index can overwrite the return value , Generally, the secondary index will be selected to find .
For example, I add a field to the table above :f4.
Now let's execute SELECT * FROM yes.t1 where f3 = 11;
:
You can see , At this time, the secondary index is not used , Instead, the whole table is scanned directly .
The reason is that the secondary index cannot overwrite the return value , Because there are more f4 Field of .
If used idx_f2_f3 This index , Want to get f4 Value , You also need to find the primary key in the primary key index , Then we can get f4 Value , In this way, the efficiency of returning the table is low .
Of course, the result is not certain , Sometimes it is possible MySQL The statistics are wrong , Wrong index . because MySQL The execution plan is selected based on cost , Yes I/O Cost and CPU cost , Use whichever index has a low cost .
therefore , In the next article, I will analyze how the cost is calculated !
Last
I believe you have fully understood what is the use of the index ?
Okay , That's all for today's article , Wait for my next article MySQL Cost analysis !
You can click on the business card below , Pay attention to me ~
I am a yes, From a little to a billion, I'll see you next .
边栏推荐
- [interview arrangement] 0211 game engine server
- 苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
- C # realizes the communication between Modbus protocol and PLC
- Revit secondary development - modify wall thickness
- 关于海康ipc的几个参数
- Aspose. Words merge cells
- [azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
- 「开源摘星计划」Loki实现Harbor日志的高效管理
- ASP. Net core introduction V
- Line test - graphic reasoning - 3 - symmetric graphic class
猜你喜欢
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Leetcode19. Delete the penultimate node of the linked list [double pointer]
PHP method of obtaining image information
Force deduction - question 561 - array splitting I - step by step parsing
行测-图形推理-8-图群类
0-5VAC转4-20mA交流电流隔离变送器/转换模块
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
LeetCode203. Remove linked list elements
LeetCode144. Preorder traversal of binary tree
“拧巴”的早教行业:万亿市场,难出巨头
随机推荐
Time convolution Network + soft threshold + attention mechanism to realize residual life prediction of mechanical equipment
「开源摘星计划」Loki实现Harbor日志的高效管理
Get the week start time and week end time of the current date
行测-图形推理-4-字母类
Matplotlib快速入门
Debezium系列之:支持 mysql8 的 set role 语句
Cataloger integrates lidar and IMU for 2D mapping
Loki, the "open source star picking program", realizes the efficient management of harbor logs
ASP. Net core introduction V
Select sort (illustration +c code)
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
6-3 find the table length of the linked table
全面掌控!打造智慧城市建设的“领导驾驶舱”
C development - interprocess communication - named pipeline
Variables and constants
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
Revit secondary development - intercept project error / warning pop-up
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Ni9185 and ni9234 hardware settings in Ni Max