在MySQL中按NULL排序 [英] ORDER BY NULL in MySQL
问题描述
MySQL中的ORDER BY NULL
是什么?
What is ORDER BY NULL
in MySQL?
它会降低查询速度吗?
推荐答案
这是为了提高性能;在GROUP BY
子句之后添加ORDER BY NULL
将使您的查询更快.
It's for performance; adding ORDER BY NULL
after a GROUP BY
clause will make your query faster.
手册中的说明:
默认情况下,MySQL对所有
GROUP BY col1, col2, ...
查询进行排序,就像在查询中指定了ORDER BY col1, col2, ...
一样.如果您包含一个包含相同列列表的显式ORDER BY
子句,则MySQL会对其进行优化,而不会造成任何速度损失,尽管排序仍然会发生.如果查询包含GROUP BY
,但您希望避免对结果进行排序的开销,则可以通过指定ORDER BY NULL
来抑制排序.例如:
By default, MySQL sorts all
GROUP BY col1, col2, ...
queries as if you specifiedORDER BY col1, col2, ...
in the query as well. If you include an explicitORDER BY
clause that contains the same column list, MySQL optimizes it away without any speed penalty, although the sorting still occurs. If a query includesGROUP BY
but you want to avoid the overhead of sorting the result, you can suppress sorting by specifyingORDER BY NULL
. For example:
INSERT INTO foo
SELECT a, COUNT(*) FROM bar GROUP BY a ORDER BY NULL;
本文介绍了作者成功优化了慢速利用此技巧进行查询,并附带EXPLAIN
输出的相关部分.
This article describes the author successfully optimising a slow query by exploiting this trick, complete with the relevant parts of the EXPLAIN
output.
这篇关于在MySQL中按NULL排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!