View the size of mysql tables that are too heavy

Note: This article is a reminder to my future self, so that next time I don't have to Google it again.

In some cases it is interesting to know why your Drupal database weighs several GB, and knowing which are the tables that weigh more can tell us what may be the problem. It is not normal to have such large databases, and it will surely give us an idea of which custom module is messing it up.

With this sql command you get a list of the tables sorted in order from most to least weight and showing the weight in MB.

SELECT table_name AS "Tables", 
       round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" 
FROM information_schema.TABLES 
WHERE table_schema = "drupal"
ORDER BY (data_length + index_length) DESC;

You have to replace in table_schema "drupal" by the name of the database we want to query.

In my case, in my local environment my database is always called "drupal."

Need a Drupal Expert?

Senior Drupal developer, freelance, specialized in what's hardest: migrations, multilingual sites, SaaS platforms and Stripe integration. I leverage AI to cut delivery times and costs, with expert review on every line of code.

No agency, no middlemen. Direct contact with the one who does the work.