来自 MySQL 文档 here
Any index that does not span all AND levels in the WHERE clause is not used to optimize the query. In other words, to be able to use an index, a prefix of the index must be used in every AND group.
这到底是什么意思?这是否意味着对于要使用的索引,AND 查询的每个组件都必须引用该索引?
假设我们有一个 Person
表,其中包含 SID (primary)
、first_name (index)
、last_name
.
这是否意味着对于以下查询
从 first_name='foo' 和 last_name='bar' 的 Person 中选择 *
不会在 first_name
上使用索引吗?
最佳答案
AND
组 是一组与AND
组合的比较。如果 WHERE
子句使用 OR
组合其中的几个,则它有多个 AND 组,例如
WHERE (col1 = 1 AND col2 = 2 AND col6 = 10) OR (col1 = '5' AND col4 = 'B' AND col2 = 16)
有两个 AND 组。一组测试 col1
、col2
和 col6
,另一组测试 col1
、 col4
和 col2
。
因此,如果索引的前缀在每个组中都经过测试,则可以使用该索引。例如,可以使用 (col1, col2, col3)
上的索引,因为前缀 (col1, col2)
跨越两个组。
关于mysql - 澄清MySQL索引要求是 "spanning"of all "and groups",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778142/