没有ORDER BY的查询速度较慢?

时间:2016-05-16 12:02:09

标签: mysql sql-order-by innodb

我在INNODB数据库上执行以下操作


没有订购的查询

SELECT  SQL_NO_CACHE second_designer
    FROM  itemrow FORCE INDEX(second_designer)
    WHERE  category like '%'
      and  type like '%'
      and  availability like '%'
    GROUP BY  second_designer

259 results in 0.0286 seconds.


按DESC订购查询

SELECT  SQL_NO_CACHE second_designer
    FROM  itemrow FORCE INDEX(second_designer)
    WHERE  category like '%'
      and  type like '%'
      and  availability like '%'
    GROUP BY  second_designer
    ORDER BY  second_designer DESC 

259 results in 0.0008 seconds.


INDEX

ADD INDEX `second_designer` (`second_designer ` , `availability`,
                             `category`, `type`) USING BTREE

EXPLAIN

id select_type table   type  possible_keys   key         key_len ref  rows Extra
1  SIMPLE      itemrow index second_designer second_designer 608 NULL 44521 Using where; Using index


为什么使用order by子句的查询比没有order by子句的查询快得多?

2 个答案:

答案 0 :(得分:0)

差异是由'hidden'LIMIT phpMyAdmin添加到所有查询引起的。

$cfg['MaxRows'] Listed in phpMyAdmin Docs

明确设置LIMIT后,查询执行相同的操作(259结果为0.0286秒)。

答案 1 :(得分:0)

当我们使用order by它时使用索引来对结果行进行排序。在您的查询中没有按索引排序不使用。这是差异查询通过

按顺序运行得更快