码迷,mamicode.com
首页 > 数据库 > 详细

Mysql Innodb 表碎片整理

时间:2019-01-03 19:23:05      阅读:413      评论:0      收藏:0      [点我收藏+]

标签:test   tables   利用   mat   磁盘   format   不同   count   位置   

一、为什么会产生碎片

简单的说,删除数据必然会在数据文件中造成不连续的空白空间,而当插入数据时,这些空白空间则会被利用起来.于是造成了数据的存储位置不连续,以及物理存储顺序与理论上的排序顺序不同,这种是数据碎片.实际上数据碎片分为两种,一种是单行数据碎片,另一种是多行数据碎片.前者的意思就是一行数据,被分成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。

 

Mysql Innodb 表碎片整理

标签:test   tables   利用   mat   磁盘   format   不同   count   位置   

原文地址:https://www.cnblogs.com/xibuhaohao/p/10216078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!