Mysql 两个查询都使用 "Using where & Using temporary",但有速度差异

Mysql both querys use "Using where & Using temporary", but there is speed difference

我有两个查询要获取如下信息。

  1. SELECT distinct(I), O from (SELECT I, O FROM HB WHERE ID > 0) as H;
    
  2. SELECT distinct(I), O FROM HB WHERE ID > 0;
    


  1. 解释如下:

    1   PRIMARY <derived2>  ALL                 5   Using temporary
    2   DERIVED HB  range   PRIMARY PRIMARY 4       5   Using where
    
  2. 像这样:

    1   SIMPLE  HB  range   PRIMARY PRIMARY 4       5   Using where; Using temporary
    

它有一些不同,但我想使用 where 和 temporary。所以它必须得到相同的结果。

但是 1. 比 2 慢很多。有什么区别?

提前致谢。

查询 1 有 两个 个临时对象(尽管它没有这么说):一个用于子查询,一个用于 DISTINCT。因此查询 1 较慢。

另一件需要注意的事情:Using filesort, Using temporary 通常出现在 EXPLAIN 第一 行,即使它确实适用于其他行。

有关 EXPLAIN 中的更多详细信息,请使用 EXPLAIN FORMAT=JSON SELECT ...