MYSQL order By on count(*) 对性能有影响吗?

MYSQL has orderBy on count(*) some impact on perfomance?

我有一个计算记录数的简单查询,它是一个带有一些内部联接和一些条件的普通查询。

像这样。

select count(*)
from ......
where ....
order by .........at most 4 fields.

我的问题是按ascdesc最多4个字段的顺序对性能有影响吗?或者只是被引擎忽略或优化。

抱歉,如果问题是简单的或简单的问候。

在 mysql 文档中 http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html

In some cases, MySQL can use an index to satisfy an ORDER BY clause without doing extra sorting.

The index can also be used even if the ORDER BY does not match the index exactly, as long as all unused portions of the index and all extra ORDER BY columns are constants in the WHERE clause

在您使用 count 的情况下,不应使用索引(因为它不是索引的一部分),然后在某种程度上,这种排序方式会对性能产生影响。

关于你的问题没有被忽略,也没有被引擎优化

首先,我要注意您的查询,如所写,将 return 正好是一行。您有一个没有 GROUP BY 的聚合函数。在这种情况下,ORDER BY 基本上是空操作(我不知道 MySQL 是否通过了一行的议案)。

一般来说,order by 的性能影响取决于 行数 ,而不是 键数

我只能想到 order by 对性能影响最小的两种情况:

  • 可以使用索引进行排序。
  • 它遵循 GROUP BY 并使用聚合键(这仅在 MySQL 中为真,它对 GROUP BY 进行排序)。

当然,在几行(例如 4 )上的 ORDER BY 在性能方面几乎可以忽略不计。

不过,影响与键的大小关系不大,而与行数和行的总体大小有关。使用多个连接和 WHERE 子句,您的查询不太可能(但并非不可能)为 ORDER BY.

使用索引