brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Data_free in SHOW TABLE STATUS tells you whether OPTIMIZE TABLE will shrink a database table

Database
~/tips/tip-database-table-status-data-free-optimize

SHOW TABLE STATUS LIKE 'orders'\G

Checks how much reclaimable space a table holds before rebuilding it to shrink it.

After deleting millions of rows from a log table, the file on disk stays just as big. Before you rebuild the table, check how much there is to win.

SHOW TABLE STATUS LIKE 'orders'\G

Look at Data_free. It is the allocated space, in bytes, that is not used by rows. A large value next to a modest Data_length means the table file is mostly empty pages. Then a rebuild gives that space back:

OPTIMIZE TABLE orders;

For InnoDB the reply says Table does not support optimize, doing recreate + analyze instead. That note looks like an error but is normal: it rebuilds the table into a fresh file and updates the index statistics.

Some things to know before you run it. With innodb_file_per_table off, the space goes back into the shared tablespace, not to the filesystem. The rebuild needs free disk space about the size of the table, because it writes a full new copy first. It also takes time and a lot of I/O on a big table, so do it in a quiet hour. And if the table will grow back to the same size next month, shrinking it only gives you work; I’d only bother after a one-time cleanup.

Further reading: OPTIMIZE TABLE, SHOW TABLE STATUS

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link