MySQL is an Relational Database Management System, widely used as database system for Linux systems. This article will help you to calculate size of tables and database in MySQL or MariaDB servers though sql queries. MySQL stored all the information related to tables in database in information_schema database. We will use information_schema table to find tables and databases size.
Check Single Database Size in MySQL:
This query will calculate size of single database in mysql server. Please change ëmydbë with your actual database name.
SELECT table_schema “Database Name”, SUM( data_length + index_length)/1024/1024
“Database Size (MB)” FROM information_schema.TABLES where table_schema = ‘mydb’;
+—————+——————–+
| Database Name | Database Size (MB) |
+—————+——————–+
| mydb | 0.15625000 |
+—————+——————–+
1 row in set (0.04 sec)
Check ALL Database Size in MySQL:
This query will calculate size of all databases in mysql server.
SELECT table_schema “Database Name”, SUM(data_length+index_length)/1024/1024
“Database Size (MB)” FROM information_schema.TABLES GROUP BY table_schema;
+——————–+——————–+
| Database Name | Database Size (MB) |
+——————–+——————–+
| demodb | 0.15625000 |
| information_schema | 0.00976563 |
| mydb | 0.15625000 |
| mysql | 0.81098557 |
| performance_schema | 0.00000000 |
+——————–+——————–+
5 rows in set (0.01 sec)
Check Single Table Size in MySQL Database:
This query will calculate size of single table in a database in mysql server. Please change ëmydbë with your actual database name and ëtable_oneë with your actual table name.
SELECT table_name “Table Name”, table_rows “Rows Count”, round(((data_length + index_length)/1024/1024),2)
“Table Size (MB)” FROM information_schema.TABLES WHERE table_schema = “mydb” AND table_name =”table_one”;
+———————+————+—————–+
| Table Name | Rows Count | Table Size (MB) |
+———————+————+—————–+
| archive_one | 8 | 0.09 |
+———————+————+—————–+
1 row in set (0.00 sec)
Check All Table Size in MySQL Database:
This query will calculate size of all tables in a database in mysql server. Please change ëmydbë with your actual database name. It will also list number of rows in each table.
SELECT table_name “Table Name”, table_rows “Rows Count”, round(((data_length + index_length)/1024/1024),2)
“Table Size (MB)” FROM information_schema.TABLES WHERE table_schema = “mydb”;
+———————-+————+—————–+
| Table Name | Rows Count | Table Size (MB) |
+———————-+————+—————–+
| table_one | 8 | 0.09 |
| table_two | 0 | 0.02 |
| table_three | 0 | 0.02 |
| table_four | 174 | 0.03 |
+———————-+————+—————–+
4 rows in set (0.00 sec)