How to Check MySQL Database and Table Sizes

Roman Agabekov - May 20 - - Dev Community

From modest few-megabyte databases perfect for small-scale applications to behemoths spanning several terabytes designed to handle extensive enterprise-level data – MySQL databases display a broad spectrum of sizes. The size of your database is not only a function of the amount of data it stores but also the type of data, such as textual data, numerical data, blobs, and more.

Within each database, you'll find multiple tables. Some might be small lookup tables that facilitate faster data retrieval and support database normalization. Others could be enormous tables packed with millions of records, heavily used in daily operations and central to business processes. The size of these tables is influenced by factors such as the number of columns, the data type of each column, and the overall row count.

Indexes point to data in tables, enhancing retrieval speed. However, they also significantly affect your database’s storage footprint. For example, a heavily indexed table might occupy substantially more space than the actual data would suggest due to the additional overhead introduced by maintaining these indexes.

In this article, we'll walk you through a series of SQL commands that allow you to check the size of databases, tables, and indexes directly using the MySQL command line interface. This guide is especially useful if you prefer working with SQL commands over graphical tools like MySQL Workbench or phpMyAdmin.

Checking Disk Usage for MySQL Data Directory

Before digging into specific databases and table sizes, it's helpful to know the overall disk usage of your MySQL data directory. This information typically requires server access and cannot be obtained via SQL commands. You would need to use system commands like du in Linux:
Linux command to reveal how much disk space the MySQL data directory

du -sh /var/lib/mysql
Enter fullscreen mode Exit fullscreen mode

This command reveals how much disk space the MySQL data directory, usually found at /var/lib/mysql, is taking up. This information is key for effectively managing server resources and planning any necessary expansions or optimizations.

How to Check the Size of a Specific Database

Understanding the size of a specific MySQL database is important for various reasons. You might need to track its growth, prepare for backups, or make sure it stays within your environment's storage limits. This information becomes particularly valuable when you're planning for future capacity needs or migrating databases to a new server.

To accurately determine the size of a particular MySQL database, use the following SQL query:
SQL command to determine the size of a particular MySQL database

SELECT table_schema AS "Database",
       ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.tables
WHERE table_schema = 'your_database_name'
GROUP BY table_schema;
Enter fullscreen mode Exit fullscreen mode

Replace 'your_database_name' with the name of your database. This query sums up the data and index sizes of all tables within the database, providing the total size in megabytes.

How to Check the Size of All Databases

Whether it's to ensure that none of the databases grow unexpectedly large, manage storage capacity, or perform regular health checks – knowing the size of each database can provide valuable insights into your server's overall utilization.

To retrieve the size of each database on your MySQL server, use the following SQL query:
SQL command to retrieve the size of each database on your MySQL server

SELECT table_schema AS "Database",
       ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.tables
GROUP BY table_schema;

Enter fullscreen mode Exit fullscreen mode

This SQL command collects data from the information_schema.tables table, which contains metadata about all tables in all databases. The table_schema column represents the database name, while data_length and index_length represent the size of the table data and indexes, respectively.

It sums the data_length and index_length for all tables in each database, providing a comprehensive total for each. The sum is then converted from bytes to megabytes (MB) for easier interpretation.

Checking Table Size

Large tables can slow down query performance. Checking in on your table sizes helps you identify candidates for optimization, such as indexing and partitioning. These tables might also need more time for maintenance tasks like backups and restores. Sizing information allows you to plan for these operations more effectively.

To check the size of a specific table within a database, you can run:
SQL command to check the size of a specific table within a database

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'
  AND TABLE_NAME = 'your_table_name';
Enter fullscreen mode Exit fullscreen mode

Replace 'your_database_name' and 'your_table_name' with your specific database and table name. The query calculates the total size of the table by summing the data_length (the space used by the table's data) and index_length (the space used by the table's indexes). The result is then converted from bytes to megabytes for readability.

How to List All Table Sizes from All Databases

In MySQL, it's often necessary to get a comprehensive view of the sizes of all tables across all databases, particularly for system-wide performance analysis, storage optimization, and monitoring general data growth. This kind of overview can help you quickly identify which tables are consuming the most storage space and may require intervention or reconfiguration.

To list the sizes of all tables across all databases, you can use the following SQL query:
SQL command to list the sizes of all tables across all databases

SELECT table_schema AS "Database",
       TABLE_NAME AS "Table",
                     ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.tables
ORDER BY data_length + index_length DESC;
Enter fullscreen mode Exit fullscreen mode

The query selects the table_schema (database name) and table_name, along with the sum of data_length (actual data storage) and index_length (index storage). The results are ordered by the combined size of data and index lengths in descending order, showing the largest tables first. This prioritization helps you easily spot the biggest space consumers.

Checking Index Size

Whenever indexes are added, removed, or altered, it's a good idea to check their sizes to understand their impact on storage and performance. Periodic checks of index sizes should be part of regular database maintenance – especially for databases with heavy read operations.

To specifically check the size of indexes for a table, you can modify the SQL query to focus on the index length:
SQL command to specifically check the size of indexes for a table

SELECT TABLE_NAME AS "Table",
                     ROUND((index_length / 1024 / 1024), 2) AS "Index Size (MB)"
FROM information_schema.tables
WHERE table_schema = 'your_database_name'
  AND TABLE_NAME = 'your_table_name';
Enter fullscreen mode Exit fullscreen mode

Replace 'your_database_name' and 'your_table_name' with your specific database and table name.
This shows the size of the indexes of a specific table, helping you understand the additional space used by indexes beyond the actual data storage.

. . . . . . . . . . . . . . . . . . . . . . . . . . . .