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

标签 mysql indexing

来自 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/

相关文章:

PHP mysql代码帮助解码

python - Pandas .ix 来自 Pandas for Data Analysis 的示例,重写

Javascript - 数组索引仅返回未定义

C# mysql 参数化查询

mysql - MySQL表的简单分解

c# - 在 Visual Studio Ultimate 2010 中添加 MySQL.Data 作为引用

mysql - Debian/mariadb 限制?没有任何帮助! SQLSTATE[HY000] [1135] 无法创建新线程(errno 11 "Resource temporarily unavailable");

postgresql - 如何确定在 Postgres 中使用什么类型的索引?

performance - MongoDB 多键索引写入性能下降

sql-server - 如何在 SQL Server 中创建多列唯一约束