key_buffer_size

Tuning key_buffer_size variable
May 14, 2025 • Written by ROMAN AGABEKOV
This article is about configuring the key_buffer_size variable in a server's memory settings. It advises the optimal settings to optimize performance and how to avoid severe slowdowns due to paging. It also highlights the importance of maintaining a low ratio of key_reads to key_read_requests. Additionally, there is a recommendation on how to adjust key_buffer_size.

Basic Details

The key_buffer_size variable, also called the key cache, sets the buffer size for index blocks.

Releem automatically tunes key_buffer_size and 44 other variables to improve MySQL performance. Try Releem for Free, or deepen your understanding by reading our detailed article.

key_buffer_size – Usage

The key_buffer_size variable is used with the MyISAM storage engine in earlier versions of MySQL. InnoDB replaced MyISAM starting with MySQL 5.5. This variable can be set very low if you do not use MyISAM within your servers.

When key_buffer_size is increased more memory is allocated for the MySQL index buffer. More memory means less reliance on disk I/O and improved performance. For server situations still using MyISAM, set this value at 25 to 30% of available memory.

This variable will have a minimal performance impact for most users, as the majority of servers have very few MyISAM tables.

key_buffer_size – Configuration

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

Command Line:
mysqld> set global key_buffer_size = XX

Replace XX with 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:
[mysqld]
key_buffer_size = XX

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

key_buffer_size - Considerations

When configuring the key_buffer_size variable, try to keep this value between 25% and 30% of the server's total memory. Be careful not to raise key_buffer_size too high (above 50%), or you will experience major slowdowns due to paging.

Ideally, keep the ratio between key_reads to key_read_requests as low as possible. This means that for every key_read from the disk, there are way more key_read_requests from memory. 1 key_read per 1000 key_read_requests is a great ratio.

Remember that the InnoDB storage engine is likely in use, and has its own memory requirements. For most modern servers, start with 1% of RAM and adjust from there.

Adjust key_buffer_size in increments or multiples of 4096 bytes or the system will warn you and then automatically select a value that matches the 4096-byte rule.

NOTE: Setting key_buffer_size to 0 will cause the key cache to not initialize at server startup. Upon changing to a non-zero number, the key cache will initialize.
FAQ:
Configuring MySQL key_buffer_size
What is key_buffer_size in MySQL?
key_buffer_size determines the size of the buffer used for indexing operations in MyISAM tables. It caches index blocks to improve read/write performance.

What is the default value of key_buffer_size?
In MySQL 8.0, the default is typically 8MB, but it can vary depending on the distribution or configuration.

What is the recommended value for key_buffer_size?
For servers primarily using MyISAM, allocate 25–50% of total memory. For InnoDB-based systems, keep it low (e.g., 8–32MB), as it’s not used by InnoDB.

How does key_buffer_size affect MyISAM performance?
A larger buffer allows more index blocks to be cached, reducing disk I/O and improving query speed for MyISAM tables.

Is key_buffer_size used with InnoDB?
No, InnoDB uses the innodb_buffer_pool_size instead. key_buffer_size only affects MyISAM and similar storage engines.

How to check current key_buffer_size in MySQL?
Run:
SHOW VARIABLES LIKE 'key_buffer_size';

How to monitor key_buffer_size efficiency?
Use:
SHOW STATUS LIKE 'Key_read%';

Evaluate Key_reads / Key_read_requests — a low ratio indicates good caching.

What happens if key_buffer_size is too small?
MySQL will frequently read index blocks from disk, causing slower queries and higher disk I/O.

Can I increase key_buffer_size dynamically?
Yes, use:
SET GLOBAL key_buffer_size = <value>;

(Only affects new connections; add to config for persistence.)

How do I choose an optimal key_buffer_size?
Analyze usage of MyISAM tables. If they’re minimal or legacy-only, keep it small. Use tools like Releem to get tuning recommendations based on actual usage.
If you're confused, let Releem take handle the configuration process and remove all uncertainty from your system variable settings.

Releem Agent will collect metrics and important system information that is transferred to the Releem Cloud Platform. Releem Cloud Platform will calculate Releem Score and make any recommendations to improve your server performance. Releem can automatically apply these settings, including any recommendations for key_buffer_size.
Releem automatically identifies MySQL performance issues, tunes configuration and optimizes SQL queries