mysql - 什么是 MySQL 覆盖索引?

标签 mysql indexing query-performance covering-index

我看到了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/

相关文章:

sql - DISTINCT 与 PARTITION BY 对比 GROUPBY

mysql - 在一次查询中计数两次

javascript - 获取 jCarouselLite 图像/页面的当前索引

mysql - MySQL 对约 400.000 个条目的查询速度缓慢

SQL Server 索引性能 - 长列

MySQL优化sql join查询

postgresql - 使用 Postgres intarray 字段的后代搜索性能

php - MySQL 错误 `ON DUPLICATE KEY UPDATE`

c# - 从 SQL 结果构建表 ASP.net C#

mysql - 有没有什么最好的方法将批量数据从Mysql传输到Mongodb?