// mysql · memory

sort_buffer_size

Memory each sort may use before it writes merge passes to disk.

Default
256KB
Range
32KB to 2^64-1 bytes
Restart needed
No
Default checked on
MySQL 8.4 LTS

What it does

Each sort that cannot use an index gets its own buffer. Since MySQL 8.0.12 the buffer grows as needed up to this size instead of being allocated in full. Sort_merge_passes in SHOW GLOBAL STATUS counts sorts that did not fit.

Default value

256KB. 262144 bytes (MySQL 8.4).

Check the current value

SHOW GLOBAL VARIABLES LIKE 'sort_buffer_size';

SELECT VARIABLE_NAME, VARIABLE_SOURCE, MIN_VALUE, MAX_VALUE, SET_TIME
FROM performance_schema.variables_info
WHERE VARIABLE_NAME = 'sort_buffer_size';

Change it

SET SESSION sort_buffer_size = 2097152;   -- this connection
SET GLOBAL sort_buffer_size = 2097152;    -- new connections, until restart
SET PERSIST sort_buffer_size = 2097152;   -- new connections, and after restarts

No restart. SET GLOBAL (or SET PERSIST, which also survives restarts) applies to connections opened afterwards; existing connections keep their session value unless they SET it themselves.

When to change it

Raise it per session for a big report rather than globally, if Sort_merge_passes is high.

What can go wrong

Large global values multiply by every sorting connection.

The closest setting on other engines

EngineSettingDefaultRestart
MySQL sort_buffer_size this page 256KB No
PostgreSQL work_mem 4MB No
Oracle pga_aggregate_target 10MB or 20% of SGA No

Questions about sort_buffer_size

What is the default value of sort_buffer_size in MySQL?

256KB. 262144 bytes (MySQL 8.4).

Does changing sort_buffer_size require a restart?

No. No restart. SET GLOBAL (or SET PERSIST, which also survives restarts) applies to connections opened afterwards; existing connections keep their session value unless they SET it themselves.

When should I change sort_buffer_size?

Raise it per session for a big report rather than globally, if Sort_merge_passes is high.

Back to all MySQL settings, or check the MySQL manual for every option.

// one client for every engine

Check sort_buffer_size on your own server

SQLly runs the query above against MySQL and the other engines in this reference, from one desktop app on macOS, Windows and Linux.