更新时间:2023-08-27 12:25:34
我已经在一个表上运行了以下测试,该表具有100万个与1万个类别随机相关的产品(MariaDB 10.0.19):
I've run the following test on a table with 1M products randomly related to 10K categories (MariaDB 10.0.19):
select p.categoryId, count(*) as total
from products p
group by p.categoryId
having count(*) = 100
执行时间:156毫秒
select p.categoryId, count(*) as total
from products p
group by p.categoryId
having total = 100
执行时间:156毫秒
因此,性能似乎没有任何差异。
So there doesn't seem to be any difference in performance.
请注意,使用 ORDER BY
引擎会将结果复制到临时表中(请参见说明:使用临时表;使用文件排序
)。因此,即使您使用 ORDER BY COUNT(*)
,也无法重新计算该值。
Note that with ORDER BY
the engine will copy the result into a temporary table (See EXPLAIN: Using temporary; Using filesort
). So the value can't be recalculated, even when you use ORDER BY COUNT(*)
.
但是-当我使用 ORDER BY COUNT(DISTINGT ...)
时(我无法解释):
However - There is a difference (which I can not explain) when I use ORDER BY COUNT(DISTINGT ...)
:
select p.categoryId, count(distinct p.productData) as total
from products p
group by p.categoryId
order by total
配置文件:863毫秒,用于复制到tmp表
Profile: 863 msec for Copying to tmp table
select p.categoryId, count(distinct p.productData) as total
from products p
group by p.categoryId
order by count(distinct p.productData)
配置文件:963毫秒复制到tmp表
Profile: 963 msec for Copying to tmp table