Skip to content

Instantly share code, notes, and snippets.

@shlomi-noach
Last active October 19, 2015 11:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shlomi-noach/59c1e8624052cc8c255f to your computer and use it in GitHub Desktop.
Save shlomi-noach/59c1e8624052cc8c255f to your computer and use it in GitHub Desktop.
Bash script to calculate MySQL table dimensions on disk
#!/bin/bash
(
mysql_datadir=$(grep datadir /etc/my.cnf | head -n 1 | cut -d "=" -f 2 | cut -d "#" -f 1)
cd $mysql_datadir
for frm_file in $(find . -name "*.frm" | egrep -v "[.]/(mysql|sys|performance_schema|common_schema)")
do
table_schema=$(echo $frm_file | cut -d "/" -f 2)
table_name=$(echo $frm_file | cut -d "/" -f 3 | cut -d "." -f 1)
if [ -f ${frm_file//.frm/.MYD} ]; then
# A MyISAM table
# This script only supports an unpartitioned table
file_size=$(du -cb ${frm_file//.frm/.MY?} 2> /dev/null | tail -n 1)
elif [ -f ${frm_file//.frm/.ARZ} ]; then
# An ARCHIVE table
# This script only supports an unpartitioned table
file_size=$(du -cb ${frm_file//.frm/.ARZ} 2> /dev/null | tail -n 1)
else
# Is this InnoDB?
tbl_file=${frm_file//.frm/.ibd}
if [ -f $tbl_file ]
then
# unpartitioned table
file_size=$(du -cb $tbl_file 2> /dev/null | tail -n 1)
else
# attempt partitioned innodb table
# Yeah, maybe this isn't InnoDB at all, and this clause also catches that. Oh, well...
tbl_file_partitioned=${frm_file//.frm/#*.ibd}
file_size=$(du -cb $tbl_file_partitioned 2> /dev/null | tail -n 1)
fi
fi
file_size=${file_size//total/}
echo "$table_schema.$table_name" $file_size
done
) | sort -k 2 -nr | head -n 100 | while read entry size
do
echo $entry $size
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment