gpt4 book ai didi

mysql - 澄清MySQL索引要求是 "spanning"of all "and groups"

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:42 26 4
gpt4 key购买 nike

来自 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 组。一组测试 col1col2col6,另一组测试 col1 col4col2

因此,如果索引的前缀在每个组中都经过测试,则可以使用该索引。例如,可以使用 (col1, col2, col3) 上的索引,因为前缀 (col1, col2) 跨越两个组。

关于mysql - 澄清MySQL索引要求是 "spanning"of all "and groups",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com