Mahoor13
Mahoor13

Reputation: 5617

ORDER BY NULL in MySQL

What is ORDER BY NULL in MySQL?

Does it decrease the query speed?

Upvotes: 40

Views: 26396

Answers (5)

Dzmitry Bahdanovich
Dzmitry Bahdanovich

Reputation: 1815

Since Mysql 8.0 ORDER BY NULL doesn't improve query performance.

https://dev.mysql.com/doc/refman/8.0/en/order-by-optimization.html

Previously (MySQL 5.7 and lower), GROUP BY sorted implicitly under certain conditions. In MySQL 8.0, that no longer occurs, so specifying ORDER BY NULL at the end to suppress implicit sorting (as was done previously) is no longer necessary. However, query results may differ from previous MySQL versions. To produce a given sort order, provide an ORDER BY clause.

Upvotes: 9

zloctb
zloctb

Reputation: 11186

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)

Upvotes: -3

Haim Evgi
Haim Evgi

Reputation: 125496

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.

Upvotes: 54

C.Evenhuis
C.Evenhuis

Reputation: 26446

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.

Upvotes: 12

codingismylife
codingismylife

Reputation: 95

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.

Upvotes: -2

Related Questions