Skip to content

Instantly share code, notes, and snippets.

@Rican7
Created April 27, 2016 16:14
Show Gist options
  • Save Rican7/17303ef990dfd39f692833374f6237d6 to your computer and use it in GitHub Desktop.
Save Rican7/17303ef990dfd39f692833374f6237d6 to your computer and use it in GitHub Desktop.
Query MySQL database size meta
# Get the size meta of every accessible schema
SELECT
count(*) tables,
`table_schema`,
format(sum(`table_rows`)/1000000, 3) `rows (M)`,
format(sum(`data_length`)/(1024*1024*1024), 3) `data_size (GB)`,
format(sum(`index_length`)/(1024*1024*1024), 3) `index_size (GB)`,
format((sum(`data_length`+`index_length`))/(1024*1024*1024), 3) `total_size (GB)`,
format(sum(`index_length`)/sum(`data_length`), 3) `index_data_ratio`
FROM
`information_schema`.`TABLES`
GROUP BY
`table_schema`
ORDER BY
(sum(`data_length`+`index_length`))/(1024*1024*1024) DESC # Total Size
LIMIT 0,1000;
# Get the size meta of every table in a schema
SELECT
`table_schema`,
`table_name`,
format(`table_rows`/1000, 2) `rows (K)`,
format(`data_length`/(1024*1024), 2) `data_size (MB)`,
format(`index_length`/(1024*1024), 2) `index_size (MB)`,
format((`data_length`+`index_length`)/(1024*1024), 2) `total_size (MB)`,
format(`index_length`/`data_length`, 2) `index_data_ratio (MB)`
FROM
`information_schema`.`tables`
WHERE
`table_schema` = 'SCHEMA_NAME_HERE'
ORDER BY
(`data_length`+`index_length`)/(1024*1024) DESC # Total Size
LIMIT 0,1000;
@Rican7
Copy link
Author

Rican7 commented Apr 27, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment