Wednesday, May 22, 2013

Query to Check MySQL Database Usage

To check the disk usage of a specific MySQL database, you can use the following query. This query will give you the total disk space consumed by both the data and index files of the specified database.

Query to Check MySQL Database Usage:

SELECT table_schema AS "DB", SUM(data_length + index_length) / 1024 / 1024 AS "MB" FROM information_schema.TABLES WHERE table_schema = 'REPLACE_TO_DATABASE_NAME' GROUP BY table_schema;

Explanation:

  • table_schema: Represents the name of the database.
  • data_length: The space used by the data (tables).
  • index_length: The space used by the indexes.
  • SUM(data_length + index_length): Total space used by the data and indexes.
  • / 1024 / 1024: Converts the value from bytes to megabytes (MB).

Example:

If your database name is my_database, the query would look like this:

SELECT table_schema AS "DB", SUM(data_length + index_length) / 1024 / 1024 AS "MB" FROM information_schema.TABLES WHERE table_schema = 'my_database' GROUP BY table_schema;

Output:

This query will return the total disk space (in MB) that the specified database is using on the filesystem.