MySQL 5.6 引入的 innodb_file_format 參數決定了 InnoDB 表空間的檔案格式版本,直接影響支援的列格式與壓縮功能。本文結合實測指令與官方文檔,詳細解析兩種格式的差異、適用場景與遷移注意事項。
| 項目 | Antelope(舊格式) | Barracuda(新格式) |
|---|---|---|
| 代號來源 | 內建 InnoDB | InnoDB Plugin |
| 支援的 Row Format | Redundant、Compact | Dynamic、Compressed(並相容舊兩種) |
| 設定層級 | 配置檔 innodb_file_format | 同左,建表時另指定 ROW_FORMAT |
| 預設值 | MySQL 5.6 以前為 Antelope | 5.7+ 預設 Barracuda |
-- 查看目前設定
SHOW VARIABLES LIKE '%innodb_file%';
-- 動態修改(需重啟生效或配合 innodb_file_format_max)
SET GLOBAL innodb_file_format = Barracuda;
SET GLOBAL innodb_file_format_max = Barracuda;
參數說明:
innodb_file_format:新建表預設使用的檔案格式。innodb_file_format_max:歷史上曾使用過的最高格式,決定啟動時的相容性檢查門檻。innodb_file_format_check:啟動時檢查現有表格式是否受支援,建議保持 ON。innodb_file_per_table:獨立表空間開關,Barracuda 功能(壓縮、Dynamic)需開啟此項。| Row Format | 所屬格式 | 特點 | 適用場景 |
|---|---|---|---|
| Redundant | Antelope | 舊版相容,欄位長度偏移列表,儲存較冗餘 | 極舊版本相容 |
| Compact | Antelope | 非 NULL 變長欄位長度列表,節省空間 | 通用預設(5.6 前) |
| Dynamic | Barracuda | 長欄位(BLOB/TEXT)全溢位儲存,索引不含前綴 | 大量長文字/二進位欄位 |
| Compressed | Barracuda | 頁面級壓縮(zlib),KEY_BLOCK_SIZE 控制壓縮塊大小 | IO 瓶頸、大量文字資料、SSD 環境 |
引用 Facebook 實戰經驗與 Percona 基準測試:
ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4/8。Dynamic 或 Compact。建議配置模板(my.cnf):
[mysqld]
innodb_file_format = Barracuda
innodb_file_per_table = 1
innodb_strict_mode = 1 -- 嚴格模式,防止靜默降級
-- 未開啟 Barracuda 時建立 COMPRESSED 表會報警告並退回 COMPACT
CREATE TABLE t1 (c1 INT) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
-- Warning: ROW_FORMAT=COMPRESSED requires innodb_file_format > Antelope
-- 開啟 Barracuda 後
SET GLOBAL innodb_file_format = Barracuda;
SET GLOBAL innodb_file_format_max = Barracuda;
CREATE TABLE t2 (c1 INT) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
-- 成功,.ibd 檔案明顯縮小
實測空表:Compact 96KB vs Compressed 64KB;大量資料下差距更明顯。
innodb_file_format_check=ON 時,偵測到不支援格式會拒絕啟動;OFF 僅警告但極度危險(恢復階段可能損壞資料)。ROW_FORMAT 高於 innodb_file_format,建表失敗。最佳實踐:保持 innodb_file_format_check=ON,若必須關閉,需設定 innodb_fast_shutdown=0 確保乾淨關機。
ROW_FORMAT 相關 DDL)。ALTER TABLE t ROW_FORMAT=DYNAMIC; 或 COMPRESSED。Innodb_compression_failure_rate_pct 指標,調整 KEY_BLOCK_SIZE。