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_s...
Musings on Tests, Quality, Tools, Projects and more. By Steve Page