Monday, November 23, 2020

MySQL DB-size, tables size


Size of all tables in particular DB (change NEEDED_DB_NAME to needed):
SELECT table_name AS "Table", 
data_length+index_length AS "Size in bytes" 
FROM information_schema.TABLES 
WHERE table_schema = "NEEDED_DB_NAME" 
ORDER BY (data_length + index_length) DESC;

Size of all DB in MySQL:
SELECT table_schema AS "Database", 
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" 
FROM information_schema.TABLES 
GROUP BY table_schema;

Count of rows of all tables in particular DB ((change NEEDED_DB_NAME to needed):
SELECT table_name AS "Table", 
table_rows AS "Rows" 
FROM information_schema.TABLES 
WHERE table_schema = "NEEDED_DB_NAME" 
ORDER BY table_rows DESC;

Table size description (depends on system block size):
  1. index_length=data_length=16384 (total size is 32768) - empty table with index
  2. index_length=0, data_length=16384 - empty table without index (only primary key can exist)
In MySQL (INNODB):
KEY=INDEX
PRIMARY KEY=special case of UNIQUE KEY (identifies that row and also data can't be NULL)

To check index, engine, index_length, data_length, create_time (change NEEDED_TABLE_NAME):
show table status like 'NEEDED_TABLE_NAME';

No comments:

Post a Comment