MySQL Database Size: Query and Browser Methods
In this article
To get the size of your MySQL databases, you must query the information_schema.TABLES table, as MySQL does not provide a single "size" command for a whole database. For those using a browser or GUI tool like phpMyAdmin or MySQL Workbench, the size is typically displayed in the "Databases" or "Schema" overview tab without needing to write a manual query.
Querying Database Size via SQL
Since MySQL stores metadata about every table in the information_schema, you can calculate the total size of a database by summing the data length and index length of all tables belonging to that schema. The data length represents the actual row data, while the index length represents the space used by keys and indexes.
To find the size of all databases on your server in megabytes (MB), 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, you can add a WHERE clause to filter the results:
SELECT SUM(data_length + index_length) / 1024 / 1024 AS "Database Size MB"
FROM information_schema.TABLES
WHERE table_schema = 'your_database_name';
Using Browser-Based GUI Tools
Many administrators prefer a visual browser over a command-line interface. These tools execute the information_schema queries in the background and present the data in a readable table.
- phpMyAdmin: This is the most common web-based browser. Once logged in, click the "Databases" tab at the top of the home screen. A list of all databases will appear with a column explicitly labeled "Size".
- MySQL Workbench: In the "Navigator" pane on the left, right-click a schema and select "Schema Inspector". The "General" tab provides a detailed breakdown of the total size, including data and index space.
- HeidiSQL / DBeaver: These desktop browsers usually show the size in the "Database" or "Properties" tab when a specific schema is highlighted in the object tree.
Understanding Data vs. Index Size
When viewing database sizes, it is important to distinguish between the different types of space being consumed. A database is not just a collection of rows; it is a complex structure of pointers and sorted lists.
- Data Length: This is the actual space occupied by the records in your tables. If you have a table with millions of rows of text, this value will be high.
- Index Length: Indexes are used to speed up searches. However, every index you add to a table increases the total size of the database. In some cases, especially with many small columns and multiple indexes, the index size can exceed the data size.
- Free Space (Data_free): You may notice a
data_freecolumn ininformation_schema. This represents space that was previously used but has been marked as available after deletions or updates. This is often referred to as "fragmentation".
Common Problems and Misconceptions
A frequent point of confusion is the difference between the logical size (what the query tells you) and the physical size (the space used on the hard drive).
The SQL queries mentioned above calculate the logical size of the data stored. However, the physical files on the disk (such as ibdata1 or .ibd files in InnoDB) may be larger than the sum of the tables. This happens because MySQL allocates space in "extents" and does not always shrink the physical file on disk immediately after data is deleted.
Another common issue is permission errors. To query the information_schema, the user account must have the necessary privileges. If you are using a restricted user account, you may only see the sizes of the databases you have explicit access to, rather than the entire server.
Optimizing Database Space
If you find that your database size is unexpectedly large, there are several ways to reclaim space and improve performance.
- Run OPTIMIZE TABLE: For InnoDB tables, this command rebuilds the table to reclaim unused space (the
data_freementioned earlier) and defragments the indexes. - Review Indexing: Remove redundant or unused indexes. While indexes speed up reads, they consume significant disk space and slow down write operations.
- Change Data Types: Ensure you are using the most efficient data types. For example, using
INTwhenTINYINTwould suffice, or usingVARCHARinstead ofTEXTfor short strings, can reduce the overall footprint. - Archive Old Data: Move historical data that is no longer frequently accessed to a separate archive database or a cold storage file.
Frequently Asked Questions
Why is my disk usage higher than the MySQL query result?
This is usually due to the InnoDB tablespace. MySQL allocates space in chunks, and when you delete data, the space is marked as available for new MySQL data but is not returned to the operating system. To shrink the actual file, you often have to export the data and re-import it into a fresh database.
Can I check the size of a single table?
Yes, you can query information_schema.TABLES and filter by table_name. Use SELECT data_length + index_length FROM information_schema.TABLES WHERE table_schema = 'db_name' AND table_name = 'table_name';
Does the database size include logs?
No, queries to information_schema only count the data and indexes within the tables. They do not include the size of the binary logs, slow query logs, or the undo/redo logs used for crash recovery.
Is it safe to run these size queries on a production server?
Yes, querying information_schema is a read-only operation and is generally very lightweight. It will not lock your tables or disrupt the performance of your application.
How often should I check my database size?
Monitoring should be done regularly, ideally through an automated alerting system. If your database grows rapidly, you may run out of disk space, which can lead to database corruption or total service downtime.