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: one information_schema query lists the biggest tables in your database server

Database
~/tips/tip-database-information-schema-table-sizes

SELECT table_schema, table_name,

Shows which tables use the most disk space, data and indexes together, in megabytes.

When the MariaDB volume grows, I want to know which table is doing it before I look at anything else. The server already keeps those numbers.

SELECT table_schema, table_name,
       ROUND((data_length + index_length) / 1024 / 1024, 1) AS mb
FROM information_schema.TABLES
WHERE table_schema NOT IN ('mysql','sys','performance_schema','information_schema')
ORDER BY data_length + index_length DESC
LIMIT 10;

data_length is the space for the rows and index_length for the indexes, both in bytes. Adding them gives a fair size per table. The WHERE line hides the system schemas so your own tables come first. More often than not it is a log, session or cache table at the top, one that nobody ever cleans up.

Dropping the table_name column and grouping by table_schema with SUM() gives a size per database instead.

For InnoDB these numbers are estimates based on pages, and they can lag behind after big deletes. They also do not shrink when you delete rows, because InnoDB keeps the freed space for later. To compare with what is really on disk, look at the .ibd files in the data directory. If you need fresh statistics for a table, ANALYZE TABLE updates them.

Further reading: Information Schema TABLES table

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

Link