Fetch CSV of MySQL table size vs .ibd container size

This only works if you’re using innodb_file_per_table.

Purpose: import this csv quickly into google sheets (or other spreadsheet) and compare MySQL’s internal data size to the container size on disk to determine tables needing to be optimized (or “null altered”) to reclaim disk space and maybe increase performance due to defragmentation. Rule of thumb is probably something like >=10% difference may warrant action.

I wrote this loop as a one-liner dynamically / ad-hoc on the command line a couple weeks ago but made it into a configurable, yet quick-and-dirty shell script, below.

Add -u and -p arguments to MySQL CLI command if you need to, or just place a .my.cnf in your home directory and use the script as-is.


DATADIR="/path/to/datadir" # ex: /var/lib/mysql

for x in `mysql --batch -n -e "select concat(concat(table_name,'.ibd'),',',(data_length + index_length)) as total_length from information_schema.tables where table_schema = '${SCHEMANAME}';"`
	FILE=`echo $x | cut -d , -f1`;
	FSIZE=`ls -la ${DATADIR}/${SCHEMANAME}/${FILE} | awk '{print $5}'`; 
	echo ${x},${FSIZE};