MySQL 解释 : "Using index" vs. "Using index condition"

标签 mysql optimization

MySQL 5.4 documentation, on Optimizing Queries with EXPLAIN ,关于这些额外的评论是这样说的:

  • Using index

The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.

[...]

  • Using index condition

Tables are read by accessing index tuples and testing them first to determine whether to read full table rows. In this way, index information is used to defer (“push down”) reading full table rows unless it is necessary.

我是否遗漏了什么,或者这两个意思是一样的(即“没有读取行,索引就足够了”)?

最佳答案

举个例子最能说明问题:

SELECT Year, Make --- possibly more fields and/or from extra tables
FROM myUsedCarInventory
WHERE Make = 'Toyota' AND Year > '2006'

Assuming the Available indexes are:
  CarId
  VIN
  Make
  Make and Year

此查询将解释为“使用索引”,因为它根本不需要“命中”myUsedCarInventory 表本身,因为“制造和年份”索引“覆盖”了它的需要关于 WHERE 子句中与该表相关的元素

现在,想象一下,我们保持查询不变,只是在颜色上添加了一个条件

...
WHERE Make = 'Toyota' AND Year > '2006' AND Color = 'Red'

这个查询可能会用“使用索引条件”来解释(“可能”,这里是针对 Toyota + year 估计不够有选择性的情况,优化器可能决定只扫描表)。这意味着 MySQL FIRST 将使用索引来解析 Make + Year,并且它还必须在表中查找相应的行,仅用于 满足 Make + Year 条件的行。这就是有时被称为“push down optimization”的内容。

关于MySQL 解释 : "Using index" vs. "Using index condition",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1687548/

相关文章:

MYSQL 需要更快的查询

php - 使用 mysql、php 和 pdo 选择不区分大小写

mysql - mysql 数据库损坏时备份和恢复

java - 单击按钮时,我在数据库中添加了一个对象(通过 struts 2)。但不是同时反射(reflect)

java - 在 Java 中哪个更快,而使用递归方法?

javascript - 哪个 bool 运算更快? < 或 <=

algorithm - 从数据集中搜索数据而不读取每个元素

插入选择时Mysql自动增量跳转

mysql - 搜索最佳解决方案以使其( select * from table order by rand() limit 40 )运行得更快

python - 我的 FB HackerCup 代码对大输入太慢了