SHOW TABLE STATUS; -- 所有表 SHOW TABLE STATUS LIKE 'tbl_name'; -- 單表(支援通配符) SHOW TABLE STATUS LIKE 'prefix_%'\G; -- 縱向顯示,適合終端閱讀
| 欄位 | 含義 | 實戰關注點 |
|---|---|---|
| Name | 表名 | - |
| Engine | 儲存引擎 | 確認是否為 InnoDB |
| Version | 表定義版本 | - |
| Row_format | 行格式 | Dynamic/Compressed 節省空間 |
| Rows | 行數(估算值) | InnoDB 為估算,精確需 COUNT(*) |
| Avg_row_length | 平均行長度(位元組) | 評估表大小:Rows × Avg_row_length |
| Data_length | 資料總大小(位元組) | 監控表增長趨勢 |
| Max_data_length | 最大可容納資料量 | 規劃容量上限 |
| Index_length | 索引大小(位元組) | 索引膨脹檢測,比例過高需優化 |
| Data_free | 碎片空間(已分配未使用) | 大於 100MB 或佔比 > 20% 需 OPTIMIZE TABLE |
| Auto_increment | 下一個自增值 | 防止自增 ID 耗盡 |
| Create_time | 建立時間 | - |
| Update_time | 最後更新時間 | InnoDB 不更新,MyISAM 有效 |
| Check_time | 最後檢查時間 | - |
| Collation | 字符集校對規則 | 確認 utf8mb4_general_ci |
| Checksum | 校驗和 | - |
| Create_options | 建表額外選項 | partitioned、row_format 等 |
| Comment | 備註 | View 會顯示 VIEW 字樣 |
SELECT TABLE_NAME, Data_free, ROUND(Data_free / 1024 / 1024, 2) AS free_mb, ROUND(Data_free / (Data_length + Index_length) * 100, 2) AS frag_ratio FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'mydb' AND Data_free > 100*1024*1024 ORDER BY Data_free DESC;
針對碎片率高的表執行:OPTIMIZE TABLE tbl_name; 或 ALTER TABLE tbl_name ENGINE=InnoDB;
SELECT
TABLE_NAME,
Auto_increment,
CASE
WHEN Auto_increment > 4000000000 THEN 'WARNING: 近 UINT 上限'
WHEN Auto_increment > 2000000000 THEN 'NOTICE: 近 INT 上限'
ELSE 'OK'
END AS status
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'mydb' AND Auto_increment IS NOT NULL;
SELECT TABLE_NAME, ROUND(Index_length / 1024 / 1024, 2) AS idx_mb, ROUND(Data_length / 1024 / 1024, 2) AS data_mb, ROUND(Index_length / (Data_length + Index_length) * 100, 2) AS idx_ratio FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'mydb' ORDER BY idx_ratio DESC;
索引/資料比 > 50% 時需審視是否存在冗餘索引。