Mysql Innodb 表碎片整理
一、為什麼會產生碎片
簡單的說,刪除資料必然會在資料檔案中造成不連續的空白空間,而當插入資料時,這些空白空間則會被利用起來.於是造成了資料的儲存位置不連續,以及物理儲存順序與理論上的排序順序不同,這種是資料碎片.實際上資料碎片分為兩種,一種是單行資料碎片,另一種是多行資料碎片.前者的意思就是一行資料,被分成N個片段,儲存在N個位置.後者的就是多行資料並未按照邏輯上的順序排列.當有大量的刪除和插入操作時,必然會產生很多未使用的空白空間,這些空間就是多出來的額外空間.索引也是檔案資料,所以也會產生索引碎片,理由同上,大概就是順序紊亂的問題.Engine 不同,OPTIMIZE 的操作也不一樣的,MyISAM 因為索引和資料是分開的,所以 OPTIMIZE 可以整理資料檔案,並重排索引。這樣不但會浪費空間,並且查詢速度也更慢。
二、Innodb 表碎片整理
1、查看錶行數
1)行數
MariaDB [agent_platform]> select count(1) from corporationprofit_old;
+----------+
| count(1) |
+----------+
| 18198196 |
+----------+
1 row in set (54.35 sec)
2)所佔磁碟大小
9218180KB
cat test.sh
a=`ls /u02/mysql/data/agent_platform/corporationprofit_old*|xargs du|awk '{print $1}'`
m=0
for i in $a
do
echo $i
((m=$m+$i))
done
echo $m
3)查看錶的資訊
MariaDB [(none)]> select ROW_FORMAT,TABLE_ROWS,DATA_LENGTH,INDEX_LENGTH,MAX_DATA_LENGTH,DATA_FREE,ENGINE
-> from information_schema.TABLES where TABLE_SCHEMA='agent_platform' and TABLE_NAME='corporationprofit_old' limit 1;
+------------+------------+-------------+--------------+-----------------+------------+--------+
| ROW_FORMAT | TABLE_ROWS | DATA_LENGTH | INDEX_LENGTH | MAX_DATA_LENGTH | DATA_FREE | ENGINE |
+------------+------------+-------------+--------------+-----------------+------------+--------+
| Compact | 17105657 | 5187551232 | 2068905984 | 0 | 1101004800 | InnoDB |
+------------+------------+-------------+--------------+-----------------+------------+--------+
1 row in set (0.25 sec)
2、刪除部分資料
1)總共刪除了8157318行
MariaDB [agent_platform]> delete from corporationprofit_old where date_created>='2018-10-01' ;
Query OK, 8157318 rows affected (2 min 52.18 sec)
2)查看錶的行數
MariaDB [agent_platform]> select count(1) from corporationprofit_old;
+----------+
| count(1) |
+----------+
| 10040878 |
+----------+
1 row in set (11.66 sec)
3)查看錶的資訊
MariaDB [agent_platform]> select ROW_FORMAT,TABLE_ROWS,DATA_LENGTH,INDEX_LENGTH,MAX_DATA_LENGTH,DATA_FREE,ENGINE
-> from information_schema.TABLES where TABLE_SCHEMA='agent_platform' and TABLE_NAME='corporationprofit_old' limit 1;
+------------+------------+-------------+--------------+-----------------+------------+--------+
| ROW_FORMAT | TABLE_ROWS | DATA_LENGTH | INDEX_LENGTH | MAX_DATA_LENGTH | DATA_FREE | ENGINE |
+------------+------------+-------------+--------------+-----------------+------------+--------+
| Compact | 9779701 | 5360599040 | 2138177536 | 0 | 4380950528 | InnoDB |
+------------+------------+-------------+--------------+-----------------+------------+--------+
1 row in set (0.30 sec)
4)查看錶刪除後所佔磁碟大小
9218180KB,沒有改變
3、表資料碎片整理
1)表資料整理
MariaDB [agent_platform]> OPTIMIZE table corporationprofit_old;
+--------------------------------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+--------------------------------------+----------+----------+-------------------------------------------------------------------+
| agent_platform.corporationprofit_old | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| agent_platform.corporationprofit_old | optimize | status | OK |
+--------------------------------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (5 min 34.79 sec)
2)查看錶資訊
MariaDB [agent_platform]> select ROW_FORMAT,TABLE_ROWS,DATA_LENGTH,INDEX_LENGTH,MAX_DATA_LENGTH,DATA_FREE,ENGINE
-> from information_schema.TABLES where TABLE_SCHEMA='agent_platform' and TABLE_NAME='corporationprofit_old' limit 1;
+------------+------------+-------------+--------------+-----------------+-----------+--------+
| ROW_FORMAT | TABLE_ROWS | DATA_LENGTH | INDEX_LENGTH | MAX_DATA_LENGTH | DATA_FREE | ENGINE |
+------------+------------+-------------+--------------+-----------------+-----------+--------+
| Compact | 9863806 | 2996649984 | 854638592 | 0 | 6291456 | InnoDB |
+------------+------------+-------------+--------------+-----------------+-----------+--------+
3)產看整理後所佔磁碟大小
3881468KB
碎片整理後,表的大小比之前減少了5GB。