InnoDB automatically extends each secondary index by appending the primary key columns to it. Consider this table definition:
InnoDB通过添加主键列自动扩展每个辅助索引。考虑这个表的定义:
When the optimizer takes index extensions into account, it treats k_d
as (d, i1, i2)
. In this case, it can use the leftmost index prefix (d, i1)
to produce a better execution plan:
当优化器考虑到索引扩展时,它将k_d视为(d, i1, i2)。在这种情况下,它可以使用最左边的索引前缀(d, i1)来产生更好的执行计划:
In both cases, key
indicates that the optimizer uses secondary index k_d
but the EXPLAIN output shows these improvements from using the extended index:
在这两种情况下,key表示优化器使用二级索引k_d,但EXPLAIN输出显示了使用扩展索引的这些改进:
-
key_len
goes from 4 bytes to 8 bytes, indicating that key lookups use columnsd
andi1
, not justd
. -
Key_len从4字节变为8字节,这表明键查找使用列d和i1,而不仅仅是d。
-
The
ref
value changes fromconst
toconst,const
because the key lookup uses two key parts, not one. - ref值从const变成了const,const是因为键查找使用了两个关键部分,而不是一个。
-
The
rows
count decreases from 5 to 1, indicating thatInnoDB
should need to examine fewer rows to produce the result. - 行数从5减少到1,这表明InnoDB需要检查更少的行来产生结果。
-
The
Extra
value changes fromUsing where; Using index
toUsing index
. This means that rows can be read using only the index, without consulting columns in the data row. -
额外值从Using where更改;使用索引到使用索引。这意味着可以仅使用索引读取行,而不需要查阅数据行中的列。
Differences in optimizer behavior for use of extended indexes can also be seen with SHOW STATUS:
使用扩展索引的优化器行为的差异也可以通过SHOW STATUS看到:
The preceding statements include FLUSH TABLES and FLUSH STATUS to flush the table cache and clear the status counters.
上述语句包括FLUSH TABLES和FLUSH STATUS,用于刷新表缓存和清除状态计数器。
Without index extensions, SHOW STATUS produces this result:
如果没有索引扩展,SHOW STATUS将产生如下结果:
With index extensions, SHOW STATUS produces this result. The Handler_read_next value decreases from 5 to 1, indicating more efficient use of the index:
通过索引扩展,SHOW STATUS会产生这个结果。Handler_read_next值从5降到1,表明索引的使用更有效:
The use_index_extensions flag of the optimizer_switch system variable permits control over whether the optimizer takes the primary key columns into account when determining how to use an InnoDB
table's secondary indexes.
optimizer_switch系统变量的use_index_extensions标志允许优化器在决定如何使用InnoDB表的二级索引时考虑主键列。
By default, use_index_extensions is enabled. To check whether disabling use of index extensions improves performance, use this statement:
默认情况下,use_index_extensions是启用的。要检查禁用索引扩展是否会提高性能,请使用以下语句:
Use of index extensions by the optimizer is subject to the usual limits on the number of key parts in an index (16) and the maximum key length (3072 bytes).
优化器对索引扩展的使用受索引中关键部分的数量(16)和最大键长(3072字节)的通常限制。