Tuning sort_buffer_size variable

Basic Details

The sort_buffer_size variable sets the amount of memory for a buffer that is allocated for sessions performing a sort.

sort_buffer_size – Usage

A sort buffer performs sorts for some queries using ORDER BY or GROUP BY. Configuring sort_buffer_size decides how much memory will be allocated for sort queries.

Sort_buffer_size may need to be adjusted from the default if the workload requires a significant number of sort queries. Sort_buffer_size is defined on a per-session level.

sort_buffer_size – Configuration

Sort_buffer_size system variable can be configured using the command line or configuration file:

Command Line:
mysqld> set global sort_buffer_size = XX

Replace XX with a value to suit your database needs. This change will only be applied to new connections. Make a new connection to the server, and the values will update. SET GLOBAL will not persist through a server restart.

Configuration File:
sort_buffer_size = XX

Replace XX with value to suit your database needs. Must restart the server to see changes applied.

sort_buffer_size Considerations

Sort_buffer_size can affect the performance of sort queries. If the sort_merge_passes variable, the number of passes the sort algorithm has to complete during a sort, is too large, you can counter this by increasing the sort_buffer_size variable.

You may also consider increasing sort_buffer_size to speed up sort operations that cannot be further improved by query optimization or indexing.

Sort_buffer_size should be configured carefully because choosing the wrong value can reduce performance and increase memory consumption. It might even cause crashes. The appropriate value is very specific to the workload, so if you're unsure which value to choose for your servers, you should not change the default value.
With Releem you don't have to worry about picking the wrong value from sort_buffer_size and hurting your server performance. Releem Agent collects metrics and other system information. Releem Cloud Platform uses the information collected by Releem Agent and then recommends a new and improved server configuration. Releem can automatically apply these settings, including any recommendations for sort_buffer_size.