【问题标题】:Mysql query with order by use index but still gives “using where”使用索引排序的 Mysql 查询,但仍然给出“使用位置”
【发布时间】:2018-08-04 00:24:07
【问题描述】:

我遇到以下查询问题:

select * 
from testtable 
where user_id = 1 and color = 1 and size = 1 
order by created_at desc, id desc;

我使用了两个索引

  • index1 (user_id, color, size)
  • index2 (created_at, id)

但我在解释结果中得到“使用位置,使用文件排序”

然后我将索引更改为使用所有五列

  • index1 (user_id ... id)

“使用文件排序”消失了,但我仍然得到“使用位置”

为了完全使用此查询的索引,我应该采取哪些进一步的步骤?

谢谢

【问题讨论】:

    标签: mysql sql-execution-plan explain


    【解决方案1】:

    除非您特别打算从表中获取或检查所有行,否则如果 Extra 值不是 Using where

    From the MySQL docs

    所以换句话说,这是绝对正常的。

    【讨论】:

    • 您应该包含引用材料的署名链接。
    【解决方案2】:

    第一个索引用于where。如果您还想使用索引进行排序,那么您需要在(user_id, color, size, created_at, id) 上的索引。

    【讨论】:

    • 在解释中,我更正了它以包含您示例中的所有列,但我仍然得到“使用位置”
    猜你喜欢
    • 2012-03-01
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2019-11-11
    • 2010-11-11
    相关资源
    最近更新 更多