ORDER BY NULL in MySQL

情到浓时终转凉″ 提交于 2019-11-29 21:21:43
Haim Evgi

It's for performance; adding ORDER BY NULL after a GROUP BY clause will make your query faster.

An explanation, from the manual:

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;

This article describes the author successfully optimising a slow query by exploiting this trick, complete with the relevant parts of the EXPLAIN output.

C.Evenhuis

This link

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

Says it speeds up queries that use GROUP BY:

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.

Some developers used ORDER BY NULL to increase the speed of the queries using the GROUP BY clause.

The reason is that prior to MySQL 5.6 there was an implicit sort of the data when calling the GROUP BY clause. So adding ORDER BY NULL was inactivating this implicit sort and consequently making the query run faster.

Since MySQL 5.6, the implicit sorting of the GROUP BY clause is DEPRECATED http://www.tocker.ca/2013/10/21/heads-up-implicit-sorting-by-group-by-is-deprecated-in-mysql-5-6.html

Therefore the ORDER BY NULL technique is now useless.

I am soory but i can see perfomance :

mysql> SELECT *,COUNT(status) FROM big_table GROUP BY status;
+----------------+----------------------------------+--------+---------------+
| id             | hash                             | status | COUNT(status) |
+----------------+----------------------------------+--------+---------------+
| 14149924276950 | 20e2873f1026c867a1044681895130b8 |      0 |        268044 |
| 14149924273884 | 889dc604799c563783396a3cb2c688a5 |      1 |        277474 |
|  1414992427397 | 4e1769e2e64e737f37b918b834f8f696 |      2 |        279815 |
| 14149924277490 | 539b71f083fc95a93d0d4b904a56ebb2 |      3 |        290216 |
| 14149924274097 | ec694b8fc1786ea4f612dbe55d351715 |      4 |        272748 |
| 14149924272735 | 64c3d1077c3ed3ee02398896376327aa |      5 |        280785 |
| 14149924278670 | 05c143790ba4ecf73fc3be78d095c067 |      6 |        295417 |
| 14149924271189 | 79bcafe38074703a49fb372c95e3676a |      7 |        310937 |
| 14149924273279 | 29069b0fe511c160e95f98e2e2b770ac |      8 |        279338 |
| 14149924277367 | 2e72091679aa6e3d64ed3c407ceeb6d4 |      9 |        281226 |
+----------------+----------------------------------+--------+---------------+
10 rows in set (44.43 sec)

mysql> SELECT *,COUNT(status) FROM big_table GROUP BY status ORDER BY NULL;
+----------------+----------------------------------+--------+---------------+
| id             | hash                             | status | COUNT(status) |
+----------------+----------------------------------+--------+---------------+
| 14149924272735 | 64c3d1077c3ed3ee02398896376327aa |      5 |        280785 |
| 14149924277367 | 2e72091679aa6e3d64ed3c407ceeb6d4 |      9 |        281226 |
|  1414992427397 | 4e1769e2e64e737f37b918b834f8f696 |      2 |        279815 |
| 14149924278670 | 05c143790ba4ecf73fc3be78d095c067 |      6 |        295417 |
| 14149924274097 | ec694b8fc1786ea4f612dbe55d351715 |      4 |        272748 |
| 14149924271189 | 79bcafe38074703a49fb372c95e3676a |      7 |        310937 |
| 14149924276950 | 20e2873f1026c867a1044681895130b8 |      0 |        268044 |
| 14149924273279 | 29069b0fe511c160e95f98e2e2b770ac |      8 |        279338 |
| 14149924277490 | 539b71f083fc95a93d0d4b904a56ebb2 |      3 |        290216 |
| 14149924273884 | 889dc604799c563783396a3cb2c688a5 |      1 |        277474 |
+----------------+----------------------------------+--------+---------------+
10 rows in set (44.13 sec)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!