Best Practices for Configuring Optimal MySQL Memory Usage

May 3, 2016
Author
Peter Zaitsev
Share this Post:

Configuring Optimal MySQL Memory Usage
In this blog post, we’ll discuss best practices for configuring optimal MySQL memory usage.

Correctly configuring memory is critical for MySQL performance and stability. Default settings in MySQL 5.7 use very little memory, which is inefficient—but over-allocation can cause instability or crashes.

Key rule: MySQL should never cause the operating system to swap. Even small amounts of swapping activity can severely degrade performance.

Some swap usage is fine (for idle OS processes), but active swapping—visible in the si and so columns of vmstat—should be avoided.

Example: No Significant Swapping

Example: Heavy Swapping

If you’re using Percona Monitoring and Management, you can monitor swap activity directly.

Swap spikes over ~1MB/sec or sustained activity indicate memory misconfiguration.

Understanding MySQL Memory Usage

MySQL memory usage includes:

  • Global buffers
  • Per-connection buffers
  • Uncontrolled allocations (e.g., stored procedures)

Because of this complexity, it’s best to monitor actual usage via VSZ:

The 5th column shows VSZ (~11GB here).

Recommendation: Keep mysqld VSZ under ~90% of system memory.

How Much Memory to Allocate

Typically, MySQL should use no more than 90% of system memory to leave room for the OS and other processes.

Use less if:

  • Other applications share the server
  • OS caching is needed (e.g., MyISAM)
  • Workloads rely heavily on temporary files

Memory allocation considerations:

  • Workload memory: ~1–10GB depending on concurrency
  • Memory fragmentation: +10% overhead possible
  • Global buffers (e.g., buffer pool, caches)

Main components:

  • innodb_buffer_pool_size
  • key_buffer_size
  • query_cache_size
  • table_open_cache

Note: InnoDB buffer pool uses an additional 5–10% overhead.

Example Configuration

System: 16GB RAM

  • Allocate ~14.4GB to MySQL (90%)
  • Reserve 1GB for connections/workload
  • Reserve 0.4GB for misc buffers
  • Remaining ~13GB for buffer pool

Final recommendation:

Operating System Considerations

Swap

Keep swap enabled (minimum ~4GB or 25% RAM):

  • Allows OS to offload unused memory
  • Prevents OOM crashes

Reduce swap usage tendency:

OOM Killer

Prevent MySQL from being killed first:

NUMA

On multi-socket systems:

Or use:

More Resources

Posts

Webinars

Presentations

Free eBooks

Tools



Download Percona Server for MySQL Today!

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved