Skip to content

Instantly share code, notes, and snippets.

@wteuber
Last active July 1, 2024 15:09
Show Gist options
  • Save wteuber/27830f963161b37b79fa9b2232f37052 to your computer and use it in GitHub Desktop.
Save wteuber/27830f963161b37b79fa9b2232f37052 to your computer and use it in GitHub Desktop.
List MySQL tables, their respective row count and size.
SELECT
table_name AS `Table`,
table_rows AS `Row Count`,
ROUND(data_length + index_length) AS `Size (Bytes)`,
CASE
WHEN data_length + index_length < 1024 THEN CONCAT(ROUND((data_length + index_length), 2), ' B')
WHEN data_length + index_length < 1024 * 1024 THEN CONCAT(ROUND((data_length + index_length) / 1024, 2), ' KB')
WHEN data_length + index_length < 1024 * 1024 * 1024 THEN CONCAT(ROUND((data_length + index_length) / 1024 / 1024, 2), ' MB')
WHEN data_length + index_length < 1024 * 1024 * 1024 * 1024 THEN CONCAT(ROUND((data_length + index_length) / 1024 / 1024 / 1024, 2), ' GB')
ELSE CONCAT(ROUND((data_length + index_length) / 1024 / 1024 / 1024 / 1024, 2), ' TB')
END AS `Size`,
ROUND((data_length + index_length) / total_db_size * 100, 2) AS `Size (%)`
FROM
information_schema.tables,
(SELECT SUM(data_length + index_length) AS total_db_size
FROM information_schema.tables
WHERE table_schema = 'your_database_name') AS db_size
WHERE
table_schema = 'your_database_name'
ORDER BY
(data_length + index_length) DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment