我看到了documentation描述覆盖索引:
covering index
An index that includes all the columns retrieved by a query.
这是否意味着覆盖索引是一个特定的索引?
我认为覆盖索引是一种现象。
如果我按照文档的描述,那么请看下面的sql语句:
create index idx_name_age on table(id, name)
select id, name from table where id = 1
select id, name, age from table where id = 1
idx_name_age
是第一个语句中的覆盖索引,第二个语句不是。
所以我认为:覆盖索引是一种现象,而不是索引。
最佳答案
假设“覆盖”是“相对于特定 SELECT
的 INDEX 的属性。
一些例子:
select id, name from table where id = 1
INDEX(id, name) -- covering; best index
INDEX(id, name, age) -- covering, but overkill
INDEX(age, name, id) -- covering, but inefficient (might not be used)
select id, name, age from table where id = 1
INDEX(id, name, age) -- Having `id` first is optimal, but any order is "covering"
正如已经指出的,如果这是 InnoDB 并且表有 PRIMARY KEY(id)
,那么这些二级索引都不值得拥有。
SELECT a FROM tbl GROUP BY b ORDER BY c
No index is very useful since the GROUP BY and ORDER BY are not the same.
INDEX(a,b,c) -- in any order, is "covering"
INDEX(b,c,a) -- "covering", and perhaps optimal.
INDEX(b,c,a,d) -- "covering", but 'bigger'
小事成就大事。当执行SELECT COUNT(*) FROM ...
时,InnoDB(通常)会选择“最小”索引来进行计数。
另一个“规则”是避免冗余索引。
INDEX(a,b) -- Let's say you 'need' this one.
INDEX(a) -- Then this one is redundant and should be dropped.
关于mysql - 什么是 MySQL 覆盖索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57606332/