MySQL Database Size Query and Browser Tools Guide
In this article
To get the size of your MySQL databases, you must query the information_schema.tables table, as MySQL does not have a single simple command like "SHOW SIZE". The most effective query sums the data_length and index_length columns to provide the total size in megabytes or gigabytes for a specific schema or all databases on the server.
The Essential SQL Query for Database Size
Because MySQL stores metadata about its structures in a virtual database called the information_schema, you can extract precise size calculations by performing a summation. The data is stored in bytes, so dividing by 1024 multiple times converts the output into a human-readable format.
To see the size of every database on your server, use the following query:
SELECT table_schema "Database Name",
SUM(data_length + index_length) / 1024 / 1024 "Size in MB"
FROM information_schema.tables
GROUP BY table_schema;
If you only need the size of one specific database, add a WHERE clause to filter the results:
SELECT SUM(data_length + index_length) / 1024 / 1024 "Size in MB"
FROM information_schema.tables
WHERE table_schema = 'your_database_name';
How MySQL Calculates Storage
Understanding where these numbers come from is critical for database optimization. MySQL tracks two primary types of space consumption for every table:
- Data Length: This represents the actual space occupied by the rows of data within the table.
- Index Length: This represents the space used by the B-tree or Hash indexes created to speed up searches. In heavily indexed tables, the index length can sometimes exceed the data length.
It is important to note that these values represent the allocated space. If you delete a large number of rows from a table using the InnoDB engine, the data_length may not decrease immediately. This is because InnoDB maintains "fragmentation" or empty holes in the data files to be reused by future inserts, rather than shrinking the physical file on the disk immediately.
Using a Browser or GUI Tool
While raw SQL queries are the most precise method, many administrators prefer a "browser" or Graphical User Interface (GUI) to visualize storage. These tools essentially run the information_schema queries in the background and present the data in a dashboard.
- MySQL Workbench: The official tool provides a "Schema Inspector." By right-clicking a schema in the navigator and selecting "Schema Inspector," you can view the total size, number of tables, and data/index distribution.
- phpMyAdmin: A widely used web-based browser. When you click on the "Databases" tab or view the table list for a specific database, the "Size" column is automatically populated for every table and summarized at the bottom.
- HeidiSQL / DBeaver: These universal database managers provide a "Properties" or "Statistics" tab for each database that lists the total size on disk.
Common Problems with Size Accuracy
Users often notice a discrepancy between the size reported by a MySQL query and the actual size of the files on the server's hard drive. This is usually caused by several technical factors:
- ibdata1 and Shared Tablespaces: By default, older versions of MySQL stored all data in one large system tablespace file (
ibdata1). Even if you drop a database, the physical file on disk does not shrink; the space is simply marked as available for internal MySQL use. - file_per_table Setting: If
innodb_file_per_tableis enabled (which is the default in modern versions), each table gets its own.ibdfile. This makes disk-level tracking more accurate and allows for reclaiming space viaOPTIMIZE TABLE. - Binary Logs: The
information_schemaquery only measures the data and indexes. It does not include binary logs (used for replication) or slow query logs, which can consume gigabytes of disk space independently of the database size.
Comparing Query Methods vs. Disk Inspection
Depending on your goal, you may choose different ways to check size. The information_schema method is best for internal auditing, while OS-level checks are best for server capacity planning.
Query Method (Internal):
- Pros: Fast, granular (down to the table level), doesn't require root OS access.
- Cons: Does not account for log files or filesystem overhead.
Disk Method (External):
- Pros: Shows exactly how much hard drive space is gone.
- Cons: Requires SSH/Terminal access, cannot easily tell which specific table is the culprit if using a shared tablespace.
Frequently Asked Questions
Why is my database size not decreasing after deleting rows?
InnoDB does not automatically shrink the physical file on disk after a DELETE operation. It keeps the space for future data. To reclaim this space and shrink the file, you must run the OPTIMIZE TABLE table_name; command, which rebuilds the table and releases unused space to the operating system.
Does the size query include the overhead?
The basic data_length + index_length query does not include the data_free column. data_free represents the allocated space that is currently empty. To see the total space reserved by the database, including this empty space, you should add data_free to your summation.
Can I run these queries on a production server?
Yes, querying the information_schema is generally safe and lightweight. It reads metadata rather than scanning every row of your actual data, so it will not lock your tables or significantly degrade performance.
How do I find the largest table in my database?
You can modify the query to sort by size. Use SELECT table_name, (data_length + index_length) / 1024 / 1024 AS size_mb FROM information_schema.tables WHERE table_schema = 'your_db' ORDER BY size_mb DESC LIMIT 10; to see the top 10 largest tables.