Last active
April 24, 2019 14:41
-
-
Save fmmendes/b26b189ddb09b1484f181bc423e69b2f to your computer and use it in GitHub Desktop.
MySQL loose space analysis - Query to analyse the loose space on a database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
CONCAT('`', TABLE_SCHEMA, '`.`', TABLE_NAME, '`') AS `OBJECT NAME`, | |
ROUND((DATA_LENGTH / 1024 / 1024), 2) AS `OBJECT SIZE (MB)`, | |
`RATIO`, | |
ROUND((DATA_FREE / 1024 / 1024), 2) AS `LOOSE SPACE (MB)`, | |
`DDL` | |
FROM | |
( | |
SELECT | |
TABLE_SCHEMA, | |
TABLE_NAME, | |
TABLE_ROWS, | |
DATA_LENGTH, | |
DATA_FREE, | |
DATA_FREE / DATA_LENGTH AS `RATIO`, | |
CONCAT('OPTIMIZE NO_WRITE_TO_BINLOG TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '`;') AS `DDL` | |
FROM | |
information_schema.TABLES | |
WHERE | |
TABLE_SCHEMA != 'mysql' | |
AND | |
DATA_LENGTH > 0 | |
AND | |
DATA_FREE > 0 | |
) t | |
ORDER BY `RATIO` DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment