Skip to content

Instantly share code, notes, and snippets.

@xurizaemon
Created March 15, 2012 02:35
Show Gist options
  • Save xurizaemon/2041376 to your computer and use it in GitHub Desktop.
Save xurizaemon/2041376 to your computer and use it in GitHub Desktop.
Show largest tables (optionally, in a specific DB)
#!/bin/bash
if [ -z $1 ] ; then
SQL="
SELECT
CONCAT(table_schema, '.', table_name) 'Table',
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') 'Tot Size',
CONCAT(ROUND(table_rows / 1000000, 2), 'M') 'Rows',
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') 'Data',
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') 'Idx',
ROUND(index_length / data_length, 2) 'Idx Frac'
FROM information_schema.TABLES
ORDER BY data_length + index_length DESC
LIMIT 10;
"
else
SQL="
SELECT
CONCAT(table_schema, '.', table_name) 'Table',
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') 'Tot Size',
CONCAT(ROUND(table_rows / 1000000, 2), 'M') 'Rows',
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') 'Data',
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') 'Idx',
ROUND(index_length / data_length, 2) 'Idx Frac'
FROM information_schema.TABLES
WHERE table_schema = '$1'
ORDER BY data_length + index_length DESC
LIMIT 10;
"
fi
mysql -e "$SQL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment