Skip to content

How to Determine Disk Space Usage in MySQL

Updated: at 02:12 PM

How to Determine Disk Space Usage in MySQL: From Table-Level to Schema-Level Metrics

Managing disk space is crucial when working with databases. Knowing how much space each table consumes not only helps you keep tabs on your data but also aids in optimization and performance tuning. For MySQL beginners, this blog post will provide a detailed guide on how to obtain disk space metrics for individual tables within a particular schema and extend that to summarizing disk space for all schemas.

Finding Table Sizes in a Specific Schema

Prerequisites

  1. Access to MySQL Workbench or MySQL Server.
  2. Basic understanding of SQL queries.

Step-by-step Guide

  1. Launch MySQL Workbench: Open MySQL Workbench and establish a connection to your MySQL server.

  2. Open a New Query Tab: Look for a ”+” sign or an option stating “New Query Tab” and click on it.

  3. Select the Schema: Pick the schema (database) you are interested in from the list of available schemas.

  4. Execute the SQL Query: Paste the following SQL query into the query editor:

    SELECT table_name AS `Table`,
    ROUND(((data_length + index_length) / 1024 / 1024), 2) AS `Size (MB)`
    FROM information_schema.TABLES
    WHERE table_schema = "your_database_name"
    ORDER BY (data_length + index_length) DESC;

    Replace "your_database_name" with the actual name of the schema you want to inspect.

  5. Run the Query: Click the “Execute” button to run the query.

  6. Review the Results: The output will list tables in your chosen schema along with their sizes in MB, sorted in descending order.

Summarizing Disk Space for All Schemas

Moving a level higher, let’s find out how to get disk space metrics for all schemas in a MySQL instance, with a total size for each schema.

Step-by-step Guide

  1. Open a New Query Tab: If you have closed MySQL Workbench, follow the steps to open a new query tab as mentioned above.

  2. Execute the SQL Query: Enter the following SQL query:

    SELECT table_schema AS `Database`,
    ROUND(SUM((data_length + index_length) / 1024 / 1024), 2) AS `Total Size (MB)`
    FROM information_schema.TABLES
    GROUP BY table_schema
    ORDER BY SUM(data_length + index_length) DESC;
  3. Run the Query: Hit the “Execute” button to run the query.

  4. Review the Results: You will now see a summarized list, showing each schema and its total disk space consumption in MB, sorted in descending order.

Conclusion

These two methods offer a reliable way to assess disk space usage at both the table and schema levels. While the numbers might be approximations, they are sufficiently accurate for most practical purposes. These insights are invaluable for planning, optimization, and understanding your MySQL databases better.

Should you find your database growing at an unmanageable rate, or if you simply wish to understand the footprint of your data, these metrics can serve as your starting point. Armed with this information, you can make more informed decisions about managing your MySQL databases.

Happy querying!

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects