MySQL表碎片整理
MySQL表碎片整理
1. 计算碎片大小
要整理碎片,首先要了解碎片的计算方法。
可以通过show table [from|in db_name] status like '%table_name%'
命令查看:
mysql> show table from employees status like 't1'\G
*************************** 1. row ***************************
Name: t1
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 1176484
Avg_row_length: 86
Data_length: 101842944
Max_data_length: 0
Index_length: 0
Data_free: 39845888
Auto_increment: NULL
Create_time: 2018-08-28 13:40:19
Update_time: 2018-08-28 13:50:43
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
碎片大小 = 数据总大小 - 实际表空间文件大小
数据总大小 =
Data_length + Data_length
= 101842944实际表空间文件大小 =
rows * Avg_row_length
= 1176484 * 86 = 101177624碎片大小 = (101842944 - 101177624) / 1024 /1024 = 0.63MB
通过information_schema.tables
的DATA_FREE
列查看表有没有碎片:
SELECT t.TABLE_SCHEMA,
t.TABLE_NAME,
t.TABLE_ROWS,
t.DATA_LENGTH,
t.INDEX_LENGTH,
concat(round(t.DATA_FREE / 1024 / 1024, 2), 'M') AS datafree
FROM information_schema.tables t
WHERE t.TABLE_SCHEMA = 'employees' +--------------+--------------+------------+-------------+--------------+----------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_ROWS | DATA_LENGTH | INDEX_LENGTH | datafree |
+--------------+--------------+------------+-------------+--------------+----------+
| employees | departments | 9 | 16384 | 16384 | 0.00M |
| employees | dept_emp | 331143 | 12075008 | 11567104 | 0.00M |
| employees | dept_manager | 24 | 16384 | 32768 | 0.00M |
| employees | employees | 299335 | 15220736 | 0 | 0.00M |
| employees | salaries | 2838426 | 100270080 | 36241408 | 5.00M |
| employees | t1 | 1191784 | 48824320 | 17317888 | 5.00M |
| employees | titles | 442902 | 20512768 | 11059200 | 0.00M |
| employees | ttt | 2 | 16384 | 0 | 0.00M |
+--------------+--------------+------------+-------------+--------------+----------+
8 rows in set (0.00 sec)
2. 整理碎片
2.1 使用alter table table_name engine = innodb
命令进行整理。
root@localhost [employees] 14:27:01> alter table t1 engine=innodb; Query OK, 0 rows affected (5.69 sec)
Records: 0 Duplicates: 0 Warnings: 0 root@localhost [employees] 14:27:15> show table status like 't1'\G
*************************** 1. row ***************************
Name: t1
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 1191062
Avg_row_length: 48
Data_length: 57229312
Max_data_length: 0
Index_length: 0
Data_free: 2097152
Auto_increment: NULL
Create_time: 2018-08-28 14:27:15
Update_time: NULL
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
2.2 使用pt-online-schema-change工具也能进行在线整理表结构,收集碎片等操作。
[root@mysqldb1 14:29:29 /root]
# pt-online-schema-change --alter="ENGINE=innodb" D=employees,t=t1 --execute
Cannot chunk the original table `employees`.`t1`: There is no good index and the table is oversized. at /opt/percona-toolkit-3.0.11/bin/pt-online-schema-change line 5852.
需表上有主键或唯一索引才能运行
[root@mysqldb1 14:31:16 /root]
# pt-online-schema-change --alter='engine=innodb' D=employees,t=salaries --execute
No slaves found. See --recursion-method if host mysqldb1 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
analyze_table, 10, 1
copy_rows, 10, 0.25
create_triggers, 10, 1
drop_triggers, 10, 1
swap_tables, 10, 1
update_foreign_keys, 10, 1
Altering `employees`.`salaries`...
Creating new table...
Created new table employees._salaries_new OK.
Altering new table...
Altered `employees`.`_salaries_new` OK.
2018-08-28T14:37:01 Creating triggers...
2018-08-28T14:37:01 Created triggers OK.
2018-08-28T14:37:01 Copying approximately 2838426 rows...
Copying `employees`.`salaries`: 74% 00:10 remain
2018-08-28T14:37:41 Copied rows OK.
2018-08-28T14:37:41 Analyzing new table...
2018-08-28T14:37:42 Swapping tables...
2018-08-28T14:37:42 Swapped original and new tables OK.
2018-08-28T14:37:42 Dropping old table...
2018-08-28T14:37:42 Dropped old table `employees`.`_salaries_old` OK.
2018-08-28T14:37:42 Dropping triggers...
2018-08-28T14:37:42 Dropped triggers OK.
Successfully altered `employees`.`salaries`.
2.3 使用optimize table命令,整理碎片。
运行OPTIMIZE TABLE
, InnoDB创建一个新的.ibd具有临时名称的文件,只使用存储的实际数据所需的空间。优化完成后,InnoDB删除旧.ibd文件并将其替换为新文件。如果先前的.ibd文件显着增长但实际数据仅占其大小的一部分,则运行OPTIMIZE TABLE可以回收未使用的空间。
mysql>optimize table account;
+--------------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+--------------+----------+----------+-------------------------------------------------------------------+
| test.account | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| test.account | optimize | status | OK |
+--------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (0.09 sec)
3.整理表碎片shell脚本
# cat optimize_table.sh
#!/bin/sh
socket=/tmp/mysql3306.sock
time=`date +"%Y-%m-%d"`
SQL="select concat(d.TABLE_SCHEMA,'.',d.TABLE_NAME) from information_schema.TABLES d where d.TABLE_SCHEMA = 'employees'"
optimize_table_name=$(/usr/local/mysql/bin/mysql -S $socket -e "$SQL"|grep -v "TABLE_NAME")
echo "Begin Optimize Table at: "`date +"%Y-%m-%d %H:%M:%S"`>/tmp/optimize_table_$time.log
for table_list in $optimize_table_name
do
echo `date +"%Y-%m-%d %H:%M:%S"` "alter table $table_list engine=innodb ...">>/tmp/optimize_table_$time.log
/usr/local/mysql/bin/mysql -S $socket -e "alter table $table_list engine=innoDB"
done
echo "End Optimize Table at: "`date +"%Y-%m-%d %H:%M:%S"`>>/tmp/optimize_table_$time.log
输出内容
# cat optimize_table_2018-08-30.log
Begin Optimize Table at: 2018-08-30 08:43:21
2018-08-30 08:43:21 alter table employees.departments engine=innodb ...
2018-08-30 08:43:21 alter table employees.dept_emp engine=innodb ...
2018-08-30 08:43:27 alter table employees.dept_manager engine=innodb ...
2018-08-30 08:43:27 alter table employees.employees engine=innodb ...
2018-08-30 08:43:32 alter table employees.salaries engine=innodb ...
2018-08-30 08:44:02 alter table employees.t1 engine=innodb ...
2018-08-30 08:44:17 alter table employees.titles engine=innodb ...
2018-08-30 08:44:28 alter table employees.ttt engine=innodb ...
End Optimize Table at: 2018-08-30 08:44:28
MySQL表碎片整理的更多相关文章
- Mysql Innodb 表碎片整理
一.为什么会产生碎片 简单的说,删除数据必然会在数据文件中造成不连续的空白空间,而当插入数据时,这些空白空间则会被利用起来.于是造成了数据的存储位置不连续,以及物理存储顺序与理论上的排序顺序不同,这种 ...
- MySQL表碎片清理
MySQL大表清理 生产环境data库业务表base_data大小:500G,data_free:31G mysql> SELECT table_schema,table_name,data_f ...
- my30_表碎片整理
确认表的类型与存储引擎,是否全部是innodb select TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,VERSION,ROW_FORMAT,TABLE_RO ...
- astgo经常死机变慢?试试mysql数据碎片整理吧
使用SSH之类的工具或navicat链接数据库后(注意:是链接数据库后哦,不是直接SSH后就弄,这样提示命令错误的) 执行下面命令(目的是对ASTGO的数据库内除话单之外的所有表进行数据碎片整理,特别 ...
- 检查mysql表碎片化脚本
#!/bin/sh echo -n "MySQL username: " ; read username echo -n "MySQL password: " ...
- MySQL表的碎片整理和空间回收小结
MySQL表碎片化(Table Fragmentation)的原因 关于MySQL中表碎片化(Table Fragmentation)产生的原因,简单总结一下,MySQL Engine不同,碎片化的原 ...
- MYSQL优化之碎片整理
MYSQL优化之碎片整理 在MySQL中,我们经常会使用VARCHAR.TEXT.BLOB等可变长度的文本数据类型.不过,当我们使用这些数据类型之后,我们就不得不做一些额外的工作--MySQL数据 ...
- MySQL碎片整理小节--实例演示
MYSQL之磁盘碎片整理 清澈,细流涓涓的爱 数据库引擎以InnoDB为主 1.磁盘碎片是什么 InnoDB表的数据存储在页中,每个页可以存放多条记录,这些记录以树形结构组织,这棵树称为B+树. ...
- Oracle表空间碎片整理SHRINK与MOVE
整理表碎片通常的方法是move表,当然move是不能在线进行的,而且move后相应的索引也会失效,oracle针对上述不足,在10g时加入了shrink,那这个方法能不能在生产中使用呢? ...
随机推荐
- spring静态工厂方法得到单例bean
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte ...
- 数据库的 2个参数 NLS_LENGTH_SEMANTICS 说明,comment 说明
############### sample 1: NLS_LENGTH_SEMANTICS 1.数据库字符集选择的是NLS_CHARACTERSET=UTF8,如果NLS_CHARACTERSET= ...
- (转)深入浅出linux系统umask值及其对应的文件权限讲解
浅出linux系统umask值及其对应的文件权限讲解 原文:http://blog.51cto.com/oldboy/1060032 缘起:1.此文的撰写特别为感谢51cto的博客工作人员和领导,老男 ...
- (转)运维老鸟教你安装centos6.5如何选择安装包
运维老鸟教你安装centos6.5如何选择安装包 原文:http://blog.51cto.com/oldboy/1564620 近来发现越来越多的运维小伙伴们都有最小化安装系统的洁癖,因此,找老男孩 ...
- SpringBoot | 第二十一章:异步开发之异步调用
前言 上一章节,我们知道了如何进行异步请求的处理.除了异步请求,一般上我们用的比较多的应该是异步调用.通常在开发过程中,会遇到一个方法是和实际业务无关的,没有紧密性的.比如记录日志信息等业务.这个时候 ...
- 文本编辑简体中文专业版EmEditor Professional v12.0.8(12/27/2012更新)姓名+注册码
这是一个简单好用的文本编辑器,支持多种配置,自定义颜色.字体.工具栏.快捷键设置,可以调整行距,避免中文排列过于紧密,具有选择文本列块的功能(按ALT 键拖动鼠标),并允许无限撤消.重做,总之功能多多 ...
- TCP的连接和释放过程
TCP的连接和释放过程 1.三次握手的过程 1)主机A向主机B发送TCP连接请求数据包,其中包含主机A的初始序列号seq(A)=x.(其中报文中同步标志位SYN=1,ACK=0,表示这是一个TCP连接 ...
- while循环,break和continue,运算符,格式化输出
一丶while循环 while条件: 代码块(循环体) #数数 打印1-100 count = 1 while count <= 100: print(count) count += 1 执行顺 ...
- 【MATLAB】设定坐标的轴的范围
set(gca,'XLim',[0 1.5]);%X轴的数据显示范围set(gca,'XTick',[0:0.1:1.5]);%设置要显示坐标刻度set(gca,'XTickLabel',[0:0.1 ...
- 用rem实现h5页面的编写
一 静态页面的布局 将这段代码加到script中 (function(doc, win) { var docEl = doc.documentElement, resizeEvt = 'orienta ...