You are here

How fast can you sort data with MySQL ?

MySQL Performance Blog - Sat, 18/08/2007 - 5:48pm

I took the same table as I used for MySQL Group by Performance Tests to see how much MySQL can sort 1.000.000 rows, or rather return top 10 rows from sorted result set which is the most typical way sorting is used in practice.

I tested full table scan of the table completes in 0.22 seconds giving us about 4.5 Million of rows/sec. Obviously we can't get sorted result set faster than that.

I placed temporary sort files on tmpfs (/dev/shm) to avoid disk IO as a variable as my data set fits in memory anyway and decided to experiment with sort_buffer_size variable.

The minimum value for sort_buffer_size is 32K which gives us the following speed:

PLAIN TEXT SQL:
  1. mysql> SELECT * FROM gt ORDER BY i DESC LIMIT 10;
  2. +--------+------------------------------------------+
  3. | i      | c                                        |
  4. +--------+------------------------------------------+
  5. | 100000 | 635e8e8f8e3b9dc547bbd3deaadb1f297f691729 |
  6. | 100000 | 0a7750a1393e77a2871ecfb39d5032d0b0f7c37c |
  7. | 100000 | 0db0601036fb9d1d5e17631d4d1bed9149675bb3 |
  8. | 100000 | eb6d2b5ed1897bdd0ff6e22ee1b44814ffb8f912 |
  9. | 100000 | 1bff67cc134e316dad5370de38020bef818ec45c |
  10. |  99999 | 635da2e73d88dbe5f7297253680398e58d32ff65 |
  11. |  99999 | a1feec5f8ee6c6a96723a2a0b57c418bb3ced929 |
  12. |  99999 | 72b934f76863791f740b96858d5acb6a60459644 |
  13. |  99999 | 855b47aaa25054e77dcc27de5def8de1e265f371 |
  14. |  99999 | 81980bcd9dbaa565f22a93ce1faf9e9d53407f0a |
  15. +--------+------------------------------------------+
  16. 10 rows IN SET (0.56 sec)

Not bad ! Even though MySQL does not optimize "get top N sorted rows" very well it takes just 2.5 times longer than full table scan to get the data. And this is with minimum sort_buffer allowed when a lot of sort merge passes are required for sort completion:

PLAIN TEXT SQL:
  1. mysql> SHOW STATUS  LIKE "sort%";
  2. +-------------------+-------+
  3. | Variable_name     | Value |
  4. +-------------------+-------+
  5. | Sort_merge_passes | 321   |
  6. | Sort_range        | 0     |
  7. | Sort_rows         | 10    |
  8. | Sort_scan         | 1     |
  9. +-------------------+-------+
  10. 4 rows IN SET (0.00 sec)

As you can see from this show status output MySQL only counts completely sorted rows in Sort_rows variable. In this case 1.000.000 of rows were partially sorted but only 10 rows fetched from the data file and sent and only they are counted. In practice this means Sort_rows may well understate sort activity happening on the system.

Lets now increase sort_buffer_size and see how performance is affected:

PLAIN TEXT SQL:
  1. SET sort_buffer_size=100000;
  2.  
  3. mysql> SELECT * FROM gt ORDER BY i DESC LIMIT 10;
  4. 10 rows IN SET (0.44 sec)
  5.  
  6. mysql> SHOW STATUS  LIKE "sort%";
  7. +-------------------+-------+
  8. | Variable_name     | Value |
  9. +-------------------+-------+
  10. | Sort_merge_passes | 104   |
  11. | Sort_range        | 0     |
  12. | Sort_rows         | 10    |
  13. | Sort_scan         | 1     |
  14. +-------------------+-------+
  15. 4 rows IN SET (0.00 sec)

OK raising sort_buffer_size to 100K gives quite expected performance benefit, now we're just 2 times slower than table scan of the query and considering table size was about 60MB we have 120MB/sec sort speed, while 2.000.000 rows/sec is of course more relevant in this case.

Still a lot of sort merge passes lets go with even higher buffer sizes.

PLAIN TEXT SQL:
  1. SET sort_buffer_size=1000000;
  2.  
  3. mysql> SELECT * FROM gt ORDER BY i DESC LIMIT 10;
  4. 10 rows IN SET (0.70 sec)
  5.  
  6. mysql> SHOW STATUS  LIKE "sort%";
  7. +-------------------+-------+
  8. | Variable_name     | Value |
  9. +-------------------+-------+
  10. | Sort_merge_passes | 10    |
  11. | Sort_range        | 0     |
  12. | Sort_rows         | 10    |
  13. | Sort_scan         | 1     |
  14. +-------------------+-------+
  15. 4 rows IN SET (0.00 sec)
  16.  
  17. SET sort_buffer_size=10000000;
  18.  
  19. mysql> SELECT * FROM gt ORDER BY i DESC LIMIT 10;
  20. 10 rows IN SET (1.34 sec)
  21.  
  22. mysql> SHOW STATUS  LIKE "sort%";
  23. +-------------------+-------+
  24. | Variable_name     | Value |
  25. +-------------------+-------+
  26. | Sort_merge_passes | 1     |
  27. | Sort_range        | 0     |
  28. | Sort_rows         | 10    |
  29. | Sort_scan         | 1     |
  30. +-------------------+-------+
  31. 4 rows IN SET (0.00 sec)

Wait it is not right. We're increasing sort_buffer_size and number of sort_merge_passes decreases appropriately but it does not help sort speed instead it drops 3 times from 0.44sec to do 1.34sec !

Lets try it even higher to finally get rid of sort merge passes - may be it is sort merge which is inefficient with large sort_buffer_size ?

PLAIN TEXT SQL:
  1. mysql> SET sort_buffer_size=100000000;
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql> SELECT * FROM gt ORDER BY i DESC LIMIT 10;
  5. +--------+------------------------------------------+
  6. | i      | c                                        |
  7. +--------+------------------------------------------+
  8. | 100000 | eb6d2b5ed1897bdd0ff6e22ee1b44814ffb8f912 |
  9. | 100000 | 635e8e8f8e3b9dc547bbd3deaadb1f297f691729 |
  10. | 100000 | 1bff67cc134e316dad5370de38020bef818ec45c |
  11. | 100000 | 0db0601036fb9d1d5e17631d4d1bed9149675bb3 |
  12. | 100000 | 0a7750a1393e77a2871ecfb39d5032d0b0f7c37c |
  13. |  99999 | 41f091f4074717bf80d2b1a788e6a4a122057d11 |
  14. |  99999 | 049d9591ef0f584deaaf0433c0f3eda8631bdb85 |
  15. |  99999 | 72b934f76863791f740b96858d5acb6a60459644 |
  16. |  99999 | f0a42a16a41b4249da7c31f2d9556f05622a87b4 |
  17. |  99999 | 35de8ae483779e6024c51998eb5b5e69e02eb74c |
  18. +--------+------------------------------------------+
  19. 10 rows IN SET (1.55 sec)
  20.  
  21. mysql> SHOW STATUS  LIKE "sort%";
  22. +-------------------+-------+
  23. | Variable_name     | Value |
  24. +-------------------+-------+
  25. | Sort_merge_passes | 0     |
  26. | Sort_range        | 0     |
  27. | Sort_rows         | 10    |
  28. | Sort_scan         | 1     |
  29. +-------------------+-------+
  30. 4 rows IN SET (0.00 sec)

Nope. We finally got rid of sort_merge_passes but our sort performance got even worse !

I decided to experiment a bit further to see what sort_buffer_size is optimal for given platform and given query (I did not test if it is the same for all platforms or data sets) - The optimal sort_buffer_size in this case was 70K-250K which is quite smaller than even default value.

The CPU in question was Pentium 4 having 1024K of cache.

A while ago I already wrote what large buffers are not always better but I never expected optimal buffer to be so small at least in some conditions.

What do we learn from these results:

  • Benchmark your application Unfortunately general tuning guidelines can be wrong for your particular case, or generally wrong because they tend to reprint the manual which is often written based on theoretical expectations rather than supported by large amount of testing.
  • sort_merge_passes are not that bad. Setting your sort_buffer_size large enough so there is zero sort_merge_passes may not be optimal.
  • World is full of surprises I obviously did not expect to get such results, and this is not any exception. Even spending a lot of time optimizing and otherwise working with MySQL I continue to run in results which surprise me. Some are later expected others come from underlying bugs and later fixed.