sort_buffer_size
WARNING (2 rules)
Rule IDs: mem_001, mem_002
Overview
- Purpose
- Documented in the MySQL 8.4 manual as a server system variable (scope: Both). Purpose and semantics are described at the linked manual page.
- Dynamic (MySQL 8.4 reference)
- MySQL 8.4 marks this variable as dynamic (
Dynamic= Yes). Runtime changes useSET GLOBAL(global scope) orSET SESSION(session scope) — confirm syntax and persistence (SET PERSIST) in the manual. - Default value
- 262144 (256 KB) (MySQL 8.4)
- Version and product notes
- MariaDB and Percona Server may use different names, defaults, or dynamic behavior; verify their documentation.
- Documentation
- https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_sort_buffer_size
- Other vendors
What is checked
Rules that reference this variable, with their severity and what each rule detects:
- WARNING
mem_001: Consider increasing sort_buffer_size, but rarely above 2-4MB. Very large values cause memory allocator pressure. Focus on optimizing queries with ORDER BY first. - WARNING
mem_002: Keep sort_buffer_size between 256KB and 4MB. Values above 4MB often hurt more than they help due to memory allocation overhead.
Tuning guidance
- Recommended actions:
- Consider increasing sort_buffer_size, but rarely above 2-4MB. Very large values cause memory allocator pressure. Focus on optimizing queries with ORDER BY first.
- Keep sort_buffer_size between 256KB and 4MB. Values above 4MB often hurt more than they help due to memory allocation overhead.
- Trade-offs: Per-thread buffers are allocated per-session on demand. Large values waste memory when multiplied by max_connections. Small values cause disk spills for complex queries. Tune based on query profiles, not guesswork.
Example
SET GLOBAL sort_buffer_size = 524288; -- 512 KB
-- Per-session allocation; don't set too high
Always validate on a non-production instance first. Use SET PERSIST (MySQL 8.0+) for changes that should survive restarts.