首页 技术 正文
技术 2022年11月17日
0 收藏 323 点赞 4,595 浏览 3954 个字

一、为什么会产生碎片

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898