根据MySQL 文档,在 GROUP BY 之后添加 ORDER BY NULL 应该“避免对结果进行排序的开销”。如果是这样,为什么跟踪查询会慢 5 倍
SELECT COUNT(*) advert_count, category.name
FROM advert
LEFT JOIN category ON advert.category_id = category.category_id
WHERE (
advert.state_id = 2
)
GROUP BY advert.category_id
ORDER BY NULL
LIMIT 5
比查询ORDER BY advert_count
?
SELECT COUNT(*) advert_count, category.name
FROM advert
LEFT JOIN category ON advert.category_id = category.category_id
WHERE (
advert.state_id = 2
)
GROUP BY advert.category_id
ORDER BY advert_count DESC
LIMIT 5
从 phpMyAdmin 分析:
第一个查询:
Sorting for group -
Sorting result 0.000002
Sending data 12.069774
第二个查询:
Sorting for group 2.436986
Sorting result 0.000028
Sending data 0.000021
我对此感到困惑,谁能解释我那里发生了什么?