[TOC]
# 如何設置mysql innodb 表的壓縮
設置innodb 表的 壓縮
第一,mysql的版本需要大于5.5
第二,設置innodb_file_format=barracuda
第三,create table或者alter talble 增加 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;(默認的key_block_size=16)
根據經驗,一般壓縮比例可以達到30%-40%
順序不能改變, 先設置字符集給事為 innodb_file_format=barracuda,然后再建表或者修改表的compaesed
# 步驟二
---設置innodb字符集
set global innodb_file_format=Barracuda
vi /etc/my.cnf 添加
innodb_file_format=Barracuda
--修改表壓縮
alter table t row_format=COMPRESSED;
或者建表
create table t(id int,a varchar(10)) ENGINE=InnoDB ROW_FORMAT=COMPRESSED
注意:
在innodb_file_format=antelope的情況下,建立壓縮表(表結構中帶有row_format=compressed),然后在設置innodb_file_format=barracuda ,此時建立的壓縮表會忽略壓縮參數
# 實驗(先看再做)
## Antelope 字符集下 建立壓縮innodb表
~~~
mysql> show global variables like 'innodb_file_format%';
+--------------------------+----------+
| Variable_name | Value |
+--------------------------+----------+
| innodb_file_format | Antelope |
| innodb_file_format_check | ON |
| innodb_file_format_max | Antelope |
+--------------------------+----------+
3 rows in set (0.02 sec)
~~~
~~~
mysql> create table t(id int,a varchar(10)) ENGINE=InnoDB ROW_FORMAT=COMPRESSED ;
Query OK, 0 rows affected, 2 warnings (0.24 sec)
~~~
~~~
mysql> show warnings;
+---------+------+-----------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------------+
| Warning | 1478 | InnoDB: ROW_FORMAT=COMPRESSED requires innodb_file_format > Antelope. |
| Warning | 1478 | InnoDB: assuming ROW_FORMAT=COMPACT. |
+---------+------+-----------------------------------------------------------------------+
2 rows in set (0.00 sec)
~~~
查看壓縮表的狀態
~~~
mysql> show table status like 't'\G;
*************************** 1. row ***************************
Name: t
Engine: InnoDB
Version: 10
Row_format: Compact //顯示沒有壓縮
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-02-03 17:48:56
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options: row_format=COMPRESSED
Comment:
1 row in set (0.01 sec)
~~~
結論:我們也就得出字符集是innodb_file_format=antelope,是不能夠壓縮的,壓縮選項會被忽略掉
## Barracuda 字符集下 建立壓縮innodb表
修改字符集:
~~~
set global innodb_file_format=Barracuda
~~~
~~~
mysql> set global innodb_file_format=Barracuda ;
Query OK, 0 rows affected (0.00 sec)
~~~
~~~
mysql> show global variables like 'innodb_file_format%';
+--------------------------+-----------+
| Variable_name | Value |
+--------------------------+-----------+
| innodb_file_format | Barracuda |
| innodb_file_format_check | ON |
| innodb_file_format_max | Antelope |
+--------------------------+-----------+
3 rows in set (0.00 sec)
~~~
~~~
mysql> drop table t;
Query OK, 0 rows affected (0.16 sec)
~~~
~~~
mysql> create table t(id int,a varchar(10)) ENGINE=InnoDB ROW_FORMAT=COMPRESSED ;
Query OK, 0 rows affected (0.27 sec)
~~~
~~~
mysql> show table status like 't'\G;
*************************** 1. row ***************************
Name: t
Engine: InnoDB
Version: 10
Row_format: Compressed //正確壓縮
Rows: 0
Avg_row_length: 0
Data_length: 8192
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-02-03 17:53:10
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options: row_format=COMPRESSED
Comment:
1 row in set (0.00 sec)
~~~
~~~
mysql> show create table t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`id` int(11) DEFAULT NULL,
`a` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED
1 row in set (0.00 sec)
~~~
如果是這種方式不能正確的查看表是否已經壓縮
# 注意
~~~
mysql> show global variables like '%innodb_file_format%';
+--------------------------+-----------+
| Variable_name | Value |
+--------------------------+-----------+
| innodb_file_format | Barracuda |
| innodb_file_format_check | ON |
| innodb_file_format_max | Barracuda |
+--------------------------+-----------+
3 rows in set (0.00 sec)
~~~
~~~
mysql> exit
Bye
[root@dg mysql]# service mysql restart
Shutting down MySQL.. [確定]
Starting MySQL.. [確定]
~~~
~~~
mysql> show global variables like '%innodb_file_format%';
+--------------------------+-----------+
| Variable_name | Value |
+--------------------------+-----------+
| innodb_file_format | Antelope |
| innodb_file_format_check | ON |
| innodb_file_format_max | Barracuda |
+--------------------------+-----------+
3 rows in set (0.00 sec)
~~~
重啟后字符集又變了,所以要在參數文件中標明`innodb_file_format=Barracuda` ,重啟后才不會改變
注意: 我們可以通過命令來修改 innodb_file_format= Barracuda的字符集格式,但是往往我們會忽略掉,或者忘記了參數文件中曾經設置過字符集是 innodb_file_format=Antelope或者參數文件中曾經沒有設置過 innodb_file_format,
重啟后參數還原成了innodb_file_format=Antelope,而表可能會讓我們的壓縮重新回到不壓縮的狀態,所以一定要記住在參數文件中設置字符集
~~~
mysql> show global variables like '%innodb_strict_mode%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| innodb_strict_mode | OFF |
+--------------------+-------+
1 row in set (0.00 sec)
~~~
# 總結
單個表轉換的SQL語句為:
~~~
ALTER TABLE $tableName ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;
~~~
壓縮最終還是通過消耗更多的cpu資源來換取減少IO消耗,最終帶來性能的提升,如果應用是IO密集型,而不是cpu密集型,那么可以利用剩余的cpu來提升應用性能。
innodb壓縮的優劣:https://zhuanlan.zhihu.com/p/24334129
- SQL
- 名詞
- mysql
- 初識mysql
- 備份和恢復
- 存儲引擎
- 數據表損壞和修復
- mysql工具
- 數據庫操作
- 增
- 刪
- 改
- 查
- 數據類型
- 整數類型
- 小數類型
- 日期時間類型
- 字符和文本型
- enum類型
- set類型
- 時間類型
- null與not null和null與空值''的區別
- 數據表操作
- 創建
- 索引
- 約束
- 表選項列表
- 表的其他語句
- 視圖
- sql增刪改查
- sql增
- sql刪
- sql改
- sql查
- sql語句練習
- 連接查詢和更新
- 常用sql語句集錦
- 函數
- 字符函數
- 數值運算符
- 比較運算符與函數
- 日期時間函數
- 信息函數
- 聚合函數
- 加密函數
- null函數
- 用戶權限管理
- 用戶管理
- 權限管理
- pdo
- 與pdo相關的幾個類
- 連接數據庫
- 使用
- pdo的錯誤處理
- pdo結果集對象
- pdo結果集對象常用方法
- pdo預處理
- 常用屬性
- mysql編程
- 事務
- 語句塊
- mysql中的變量
- 存儲函數
- 存儲過程
- 觸發器
- mysql優化
- 存儲引擎
- 字段類型
- 三范式和逆范式
- 索引
- 查詢緩存
- limit分頁優化
- 分區
- 介紹
- 分區算法
- list分區
- range范圍
- Hash哈希
- key鍵值
- 分區管理
- 特別注意
- 分表
- 數據碎片與維護
- innodb表壓縮
- 慢查詢
- explain執行計劃
- count和max,groupby優化
- 子查詢優化
- mysql鎖機制
- 介紹
- 演示
- 總結
- 樂觀鎖和悲觀鎖
- 扛得住的mysql
- 實例和故事
- 系統參數優化
- mysql體系結構
- mysql基準測試
- 索引
- mysql的復制
- win配置MySQL主從
- mysql5.7新特性
- 常見問題
- general log
- 忘記密碼
- uodo log與redo log
- 事務隔離級別
- mysql8密碼登錄
- explain
- 高效的Tree表
- on delete cascade 總結
- mongod
- 簡介
- 集合文檔操作語句
- 增刪改查
- 索引
- 數據導入和導出
- 主從復制
- php7操作mongod
- 權限管理
- redis
- redis簡介
- 3.2版本配置文件
- 3.0版本配置文件
- 2.8版本配置文件
- 配置文件總結
- 外網連接
- 持久化
- RDB備份方式保存數據
- AOF備份方式保存數據
- 總結
- win安裝redis和sentinel部署
- 事務
- Sentinel模式配置
- 分布式鎖
- 管道
- php中redis代碼
- 發布訂閱
- slowlog
- Redis4.0
- scan和keys
- elasticsearch
- 配置說明
- 啟動
- kibana
- kibana下載
- kibana配置文件
- kibana常用功能
- 常用術語
- Beats
- Beats簡介
- Filebeat
- Packetbeat
- Logstash
- 配置
- elasticsearch架構
- es1.7
- head和bigdesk插件
- 插件大全
- 倒排索引
- 單模式下API增刪改查
- mget獲取多個文檔
- 批量操作bulk
- 版本控制
- Mapping映射
- 基本查詢
- Filter過濾
- 組合查詢
- es配置文件
- es集群優化和管理
- logstash
- kibana
- es5.2
- 安裝
- 沖突處理
- 數據備份
- 缺陷不足
- 集群管理api
- 分布式事務
- CAP理論
- BASE模型
- 兩階段提交(2PC)
- TCC (Try-Confirm-Cancle)
- 異步確保型
- 最大努力通知型
- 總結