The Problem
We want to calculate the size of each schema on our database server. We want to do this through SQL, not with a disk-based process.
Sample use-case: an established system gives separate schemas to each client. Now it wants to begin charging a small fee based on the amount of space used by each client.
MySQL Schema Sizes
We can gather this data using the information_schema in MySQL. The "tables" table holds data and index size values for the tables of each schema, so we can group and sum them.
We'll also throw in some formatting of the size, alongside the raw large integer of the total size.
I've also dropped the ORDER BY clause found in the screenshot, as for my use case above I am not concerned with that ordering of the output.
SELECT table_schema 'Database Name',
SUM(data_length + index_length) 'Size in Bytes',
sys.FORMAT_BYTES(SUM(data_length + index_length)) 'Size (Formatted)'
FROM information_schema.tables
GROUP BY table_schema;
These "memory tickler" posts are quick-hits to refresh my mind about little technical details that arise infrequently enough that I forget in between, and wind up doing an internet search every time. By posting them here, they will be easier to find as needed.)