隨著元數據使用普及,MySQL 5 內建固定名稱資料庫 information_schema,作為中心資料字典,集中儲存:
DBA 與開發者可透過標準 SQL 查詢獲取一個或多個資料庫的全方位元數據。
mysql> SELECT a.schema_name db_name, -> IFNULL(ROUND((SUM(b.data_length)+SUM(b.index_length))/1024/1024,2),0.00) total_size_mb, -> IFNULL(ROUND(((SUM(b.data_length)+SUM(b.index_length))-SUM(b.data_free))/1024/1024,2),0.00) data_used_mb, -> IFNULL(ROUND(SUM(data_free)/1024/1024,2),0.00) data_free_mb, -> IFNULL(ROUND((((SUM(b.data_length)+SUM(b.index_length))-SUM(b.data_free))/ -> ((SUM(b.data_length)+SUM(b.index_length)))*100),2),0) pct_used, -> COUNT(table_name) tables -> FROM information_schema.schemata a -> LEFT JOIN information_schema.tables b ON a.schema_name = b.table_schema -> WHERE a.schema_name != 'information_schema' -> GROUP BY a.schema_name -> ORDER BY 1;
| db_name | total_size_mb | data_used_mb | data_free_mb | pct_used | tables |
|---|---|---|---|---|---|
| gim | 432.67 | 432.67 | 0.00 | 100.00 | 16 |
| gim2 | 8.64 | 8.64 | 0.00 | 100.00 | 6 |
| mysql | 0.33 | 0.33 | 0.00 | 99.69 | 18 |
| test | 0.00 | 0.00 | 0.00 | 0.00 | 0 |
| tpcc | 126.09 | 126.09 | 0.00 | 100.00 | 9 |
關鍵點: WHERE 條件排除 information_schema 自身,避免遞迴統計。
(BKJIA.COM 教程)