Skip to content

Instantly share code, notes, and snippets.

@spalenza
Forked from xurizaemon/mysql-largest-tables.sh
Created March 27, 2017 22:31
Show Gist options
  • Save spalenza/528008311e216d7fbf2d6a51210ebed0 to your computer and use it in GitHub Desktop.
Save spalenza/528008311e216d7fbf2d6a51210ebed0 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