在MySQL中按NULL排序 [英] ORDER BY NULL in MySQL

查看:335
本文介绍了在MySQL中按NULL排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 specified ORDER BY col1, col2, ... in the query as well. If you include an explicit ORDER BY clause that contains the same column list, MySQL optimizes it away without any speed penalty, although the sorting still occurs. If a query includes GROUP BY but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying ORDER 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆