MySQL多索引优化

标签 mysql sql

我有一个关于使用多个索引优化 sql 查询的问题。

假设我有一个表“TEST”,其中包含字段“A、B、C、D、E、F”。

在我的代码 (php) 中,我使用以下“WHERE”查询:

  • 从 TEST WHERE a = 'x' and B = 'y' 中选择 (..)
  • 从 TEST WHERE a = 'x' and B = 'y' and F = 'z' 中选择 (..)
  • 从测试中选择 (..) WHERE a = 'x' and B = 'y' and (D = 'w' or F = 'z')

在运行查询时获得最佳速度的最佳方法是什么?

3 多个索引,例如 (A, B)、(A, B, F) 和 (A, B, D, F)? 还是单一的多重指标(A、B、D、F)?

我倾向于说3个索引最好,即使数据库中的索引空间会更大。 在我的问题中,我搜索最佳执行时间而不是空间。 数据库大小合理。

最佳答案

Multiple-column indexes :

MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on. If you specify the columns in the right order in the index definition, a single composite index can speed up several kinds of queries on the same table.

换句话说,定义一个覆盖与另一个索引相同的前 N 列且顺序相同的索引是对空间和计算能力的浪费。

关于MySQL多索引优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25062530/

相关文章:

php - Symfony Doctrine 架构更新未检测到可为空日期时间的更改

MySQL 连接未按要求工作

php - Propel 错误 - 未写入 mysql 表

php - 从 PHP 更新查询不起作用

sql - 使用变量替换sql文件中的文本

sql - 将行附加到 SQL 中的每个组

mysql - SUM 落后于时间的差异

java - 带有引用多个表的sql查询的preparedstatement

MySQL - 获取多个设置的最有效查询?

MySQL INSERT INTO 语句