Understanding MySQL Memory Usage with Performance Schema

MySQL Memory Usage with Performance SchemaUnderstanding how MySQL uses memory is key to tuning it for optimal performance as well as troubleshooting cases of unexpected memory usage, i.e. when you have MySQL Server using a lot more than you would expect based on your configuration settings.

Early in MySQL history, understanding memory usage details was hard and included a lot of guesswork.  Is it possible that some queries running require a large temporary table or allocated a lot of memory for stored user variables?  Are any stored procedures taking an unexpectedly high amount of memory? All could be reasons for excessive MySQL memory usage, but you would not easily see if that is just the case.

All that changed with MySQL 5.7, which added memory instrumentation in Performance Schema, and with MySQL 8.0, this instrumentation is enabled by default, so you can get this data from pretty much any running instance.

If you’re looking for current memory usage details, Sys schema provides fantastic views:

This view shows the current memory allocated overall.  You can also drill down further, by looking at memory allocated by connections coming from different hosts:

Or even check allocation by given thread_id which can be super helpful to diagnose memory-heavy transactions or queries:

Looking at current stats, though, is not very helpful to diagnose past incidents or even to answer the question of why a particular object taking so much memory is normal or excessive. For this, you better have history captured and available for trending… exactly what Percona Monitoring and Management (PMM) is designed for.

Unfortunately, as of PMM 2.11, we do not have Performance Schema Memory Instrumentation included in the release.  It is, however, quite easy to get it added using Custom Queries.

MySQL Custom Queries

Custom Queries is a great feature that allows you to get stats from a local MySQL instance using standard SQL queries and make them available together with other metrics collected by MySQL exporter.  This can be used for Performance Schema tables, Information Schema tables, or even queries on your own schema to expose data you consider relevant for your application. Check out our Custom Queries In Percona Monitoring and Management blog post for more details.

You can install the custom queries to read memory use statistics from MySQL Performance Schema this way…

when you install the MySQL Memory Usage Details dashboard from Grafana.com. Let’s see what you get by having this dashboard installed.

First, we have the block which does not have much to do with the information we capture from Performance Schema but provides some helpful background in interpreting such information:

MySQL Memory Usage Details dashboard

We can see how long the system and MySQL process have been up, how much memory the system has and how much is available, whenever there is any aggressive swapping activity going on, etc., and all of those help us to know whenever MySQL is currently using too much memory… or too little.  We can show the number of current connections, which is a common driver of high memory utilization as well as a couple of variables that can impact memory consumption.

The MySQL Memory Usage Summary shows how much memory different objects contain, as well as what the total instrumented memory allocated by the MySQL process is.

MySQL Memory Usage Summary

With hundreds of different places where memory can be allocated, even memory consumers which are not at the top can add up to a lot:

MySQL Memory Usage by host

When we have memory allocation by Host and by User, these only cover memory which can be attributed to specific connections but is a great tool to see if there are particular hosts, users, or applications that are significant memory consumers.

MySQL memory usage

You can also see memory usage details by specific accounts (a given user coming from a given host). This is as far as we go with dashboards, as specific Connection IDs (and Thread IDs) tend to change so quickly that they are not particularly helpful for long term capture.

You may also have noticed I unselected the pmm@localhost user, as it looks like this user workload triggers some bug in memory accounting and unrealistic numbers get reported (this is also something you need to keep in mind, to not trust the data you see blindly).

Finally, you can see memory allocation by different subsystems, which can be rather helpful for advanced analyses, where just the global top consumers do not provide enough information.

What do you think? Is it helpful, or would you like to see some additional visualizations added before we consider including this feature in Percona Monitoring and Management?

Share this post

Leave a Reply