How to find and debug large variables in Drupal 7

On a recent large Drupal project we were finding that the table was holding around 4 MB of data. The issue of course with this is that this is loaded into memory on each page request regardless of whether or not you use it. Another issue is that the variable table holds serialized data, and there is an additional CPU overhead of actually de-serializing the data as well.

Introducing Variable debug

So I wrote a module Variable debug that is a straight forward and simple module that attempts to do only two things (at the moment):

A list of the highest memory usage variables stored in the {variable} table sorted by highest to lowest. There is also a list of links to Drupal.org issues to help resolve some known high usage offenders. If you know of an issue that exists that aims to resolve in-efficient usage of the variable table, please raise a new issue in the issue queue for this module.

A list of all suspected orphaned variables in the variable table. This is determined by whether or not the variable is:

Not a variable provided by Drupal core

Does not start with an enabled module name

This can help you find and remove potential abandoned variables that are of no use to you and your site.

Symptoms of rogue variables

Sometimes Drupal contributed modules use the variable table as a dumping ground for large variables that really should be stored in dedicated tables. Here is an example from one of our websites using the SQL query: