There are 3 scenarios.
- If you are using MyISAM, it is easiest to just look at the filesystem and use
du -sh /var/lib/mysql/database.
- If you are using InnoDB with innodb_file_per_table set, then you can get an approximate answer using
du -sh. It is approximate because there is still some data stored in the ibdata1 file, so you will be a little on the low side. This technique also works with mixed MyISAM/InnoDB (innodb_file_per_table) databases.
- If you are using InnoDB without
innodb_file_per_table set, then you will need to look at INFORMATION_SCHEMA.
In any of the cases above, you can run the following query to get the information that you are looking for.
mysql> select table_schema, sum((data_length+index_length)/1024/1024) AS MB from information_schema.tables group by 1;
+--------------------+-----------------+
| table_schema | MB |
+--------------------+-----------------+
| prod | 298025.72448921 |
| information_schema | 0.00781248 |
| maatkit | 70.77330779 |
| mysql | 0.66873168 |
| test | 4752.31449127 |
+--------------------+-----------------+
5 rows in set (0.01 sec)
If you have a very large number of tables, it can be slow, as you have already discovered.