InnoDB 中二级索引扫描如何工作?
InnoDB 有两种类型的索引:主索引(聚集索引)和辅助索引(带有主索引键)。
当我输入扫描由二级索引索引的字段的查询时,我的问题开始了。 InnoDB是否扫描二级索引并逐条检索满足条件的记录? 如果我在二级索引中有50次命中,那么InnoDB会寻找50次主索引吗?
InnoDB has two types of indexes: primary (clustered), and secondary (with pimary index key).
When I type query that scan fields indexed by secondary index my question starts.
Is InnoDB scan secondary index and retrive one by one records that hit condition?
If I have 50 hits in secondary index InnoDB seek 50 times primary index?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您选择二级索引未覆盖的列,那么是的,它应该从表(聚集索引)中检索它们。
如果您有这种布局:
会发生以下情况:
使用
B-Tree 查找
,InnoDB
找到ca
索引中的第一条记录> 包含$some_value
然后遍历索引,逐条获取记录,直到找到第一个大于
$some_value
的记录。 p>由于索引中只包含
ca
(键列)和id
(行指针),InnoDB
需要查找<的值code>cb 在表本身中。对于索引中的每条记录,它都会获取
id
的值并在表中搜索它。由于该表实际上是id
上的聚集索引,因此使用B-Tree
搜索。但是,如果您有以下查询:
,则可以直接从索引检索所有内容,并且不会执行步骤
3
和4
。它在查询计划中显示为使用索引
。是的(关于上面的评论)
If you select columns not covered by the secondary index, then yes, it should retrieve them from the table (the clustered index).
If you have this layout:
the following happens:
Using a
B-Tree seek
,InnoDB
finds the first record in the index onca
which holds$some_value
It then traverses the index, taking records one by one, until it finds the first record greater than
$some_value
.Since only
ca
(key column) andid
(row pointer) are contained in the index,InnoDB
needs to find values ofcb
in the table itself.For each record in the index, it takes the value of
id
and searches the table for it. Since the table is in fact a clustered index onid
,B-Tree
search is used for that.However, if you have this query:
, then everything can be retrieved right from the index, and steps
3
and4
are not performed. It is shown in the query plan asusing index
.Yes (with regard to the comment above)