mysql碎片整理 提速_MySQL 优化:碎片整理
MySQL碎片就是MySQL数据文件中一些不连续的空白空间,这些空间无法再被全部利用,久而久之越来多,越来越零碎,从而造成物理存储和逻辑存储的位置顺序不一致,这就是碎片。
碎片是如何产生的
delete操作
在MySQL中删除数据,在存储中就会产生空白的空间,当有新数据插入时,MySQL会试着在这些空白空间中保存新数据,但是呢总是用不满这些空白空间。所以日积月累,亦或是一下有大量的delete操作,一下就会有大量的空白空间,慢慢的会大到比表的数据使用的空间还大。
update操作
在MySQL中更新数据,在可变长度的字段(比如varchar)中更新数据,innodb表存储数据的单位是页,update操作会造成页分裂,分裂以后存储变的不连续,不规则,从而产生碎片。比如说原始字段长度varchar(100),我们大量的更新数据长度位为50,这样的话,有50的空间被空白了,新入库的数据不能完全利用剩余的50,这就会产生碎片。
碎片到底产生了什么影响
MySQL既然产生了碎片,你可能比较豪横说磁盘空间够大,浪费空间也没事,但是这些碎片也会产生性能问题,碎片会有什么影响呢?
空间浪费
空间浪费不用多说,碎片占用了大量可用空间
读写性能下降
由于存在大量碎片,数据从连续规则的存储方式变为随机分散的存储方式,磁盘IO会变的繁忙,数据库读写性能就会下降。
找一找有哪些碎片
现在我们有一个测试库employees,在找碎片清理碎片前我们先查询一下表的数据,记录一下时间,以便后边做对比。
mysql> select count() from current_dept_emp;
+----------+
| count() |
+----------+
| 300024 |
+----------+
1 row in set (1.17 sec)
mysql> select count() from departments;
+----------+
| count() |
+----------+
| 9 |
+----------+
1 row in set (0.00 sec)
mysql> select count() from dept_emp;
+----------+
| count() |
+----------+
| 331603 |
+----------+
1 row in set (0.08 sec)
mysql> select count() from dept_emp_latest_date;
+----------+
| count() |
+----------+
| 300024 |
+----------+
1 row in set (0.49 sec)
mysql> select count() from dept_manager;
+----------+
| count() |
+----------+
| 24 |
+----------+
1 row in set (0.00 sec)
mysql> select count() from employees;
+----------+
| count() |
+----------+
| 300024 |
+----------+
1 row in set (0.09 sec)
mysql> select count() from salaries;
+----------+
| count() |
+----------+
| 2844047 |
+----------+
1 row in set (0.60 sec)
mysql> select count() from titles;
+----------+
| count() |
+----------+
| 443308 |
+----------+
1 row in set (0.11 sec)
接下来我们开始看看都有哪些碎片吧。这里介绍两种方式查看表碎片
通过表状态信息查看
show table status like '%table_name%';
mysql> show table status like 'salaries'\G;
*************************** 1. row ***************************
Name: salaries
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 2838918
Avg_row_length: 31
Data_length: 90832896
Max_data_length: 0
Index_length: 0
Data_free: 4194304
Auto_increment: NULL
Create_time: 2021-01-14 14:33:47
Update_time: 2021-01-14 14:34:42
Check_time: NULL
Collation: utf8_bin
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
datalength 表数据大小 indexlength 表索引大小
data_free 碎片大小
根据返回信息,我们知道碎片大小为4194304(单位B)
通过数据库视图信息查看
查询information_schema.tables的data_free列的值
mysql> 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 data_free
from information_schema.tables t
where t.table_schema = 'employees';
+--------------+----------------------+------------+-------------+--------------+-----------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_ROWS | DATA_LENGTH | INDEX_LENGTH | DATA_FREE |
+--------------+----------------------+------------+-------------+--------------+-----------+
| employees | current_dept_emp | NULL | NULL | NULL | NULL |
| employees | departments | 9 | 16384 | 16384 | 0.00M |
| employees | dept_emp | 331143 | 12075008 | 5783552 | 4.00M |
| employees | dept_emp_latest_date | NULL | NULL | NULL | NULL |
| employees | dept_manager | 24 | 16384 | 16384 | 0.00M |
| employees | employees | 299069 | 15220736 | 0 | 4.00M |
| employees | salaries | 2838426 | 100270080 | 0 | 4.00M |
| employees | titles | 442902 | 20512768 | 0 | 4.00M |
+--------------+----------------------+------------+-------------+--------------+-----------+
8 rows in set (0.01 sec)
根据结果显示,data_free列数据就是我们要查询的表的碎片大小内容,是4M。
如何清理碎片
找到表碎片了,我们如何清理呢?有两种方法
分析表
命令:optimize table table_name;
这个方法主要针对MyISAM引擎表使用,因为MyISAM表的数据和索引是分离的,optimize表可以整理数据文件,重新排列索引。
注意:optimize会锁表,时间长短依据表数据量的大小。
重建表引擎
命令:alter table table_name engine = innodb;
这个方法主要针对InnoDB引擎表使用,该操作会重建表的存储引擎,重组数据和索引的存储。
刚才我们查到表salaries有4M的碎片,我们清理一下salaries表碎片
mysql> alter table salaries engine = innodb;
查询一下该表的碎片是否被清理
mysql> 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 data_free
from information_schema.tables t
where t.table_schema = 'employees' and table_name='salaries';
+--------------+------------+------------+-------------+--------------+-----------+
| table_schema | table_name | table_rows | data_length | index_length | data_free |
+--------------+------------+------------+-------------+--------------+-----------+
| employees | salaries | 2838426 | 114950144 | 0 | 2.00m |
+--------------+------------+------------+-------------+--------------+-----------+
1 row in set (0.00 sec)
碎片从原来的4M清理到现在的2M。
我们看看查询表是否提高了速度
mysql> select count() from salaries;
+----------+
| count() |
+----------+
| 2844047 |
+----------+
1 row in set (0.16 sec)
速度还是提高了不少,清理碎片后提高了查询速度。
总结一下:清理表的碎片可以提高MySQL性能,在日常工作中我们可以定期执行表碎片整理,从而提高MySQL性能。
mysql碎片整理 提速_MySQL 优化:碎片整理相关推荐
- mysql 表空间收缩_mysql表碎片清理和表空间收缩
mysql表碎片清理和表空间收缩(即清理碎片后report_site_day.ibd文件磁盘空间减小,该方案基于独立表空间存储方式) OPTIMIZETABLE [tablename],当然这种方式只 ...
- 有关mysql的清理与优化_mysql优化点整理
优化sql一般步骤: 1.通过show (session 或者 global) status 来查看( 当前连接 或者 数据库上次开机以来 )的服务器状态信息,默认是session 例如: show ...
- mysql全套基础知识_Mysql基础知识整理
MySQL的查询过程 (一条sql语句在MySQL中如何执行): 客户端请求 ---> 连接器(验证用户身份,给予权限) ---> 查询缓存(存在缓存则直接返回,不存在则执行后续操作) - ...
- mysql整理类型_Mysql 时间类型整理
一.date_sub.SUBDATE.date_add select now(), date_sub(now(),interval 1 minute),SUBDATE(now(),interval ...
- mysql数据库知识点梳理_MySQL数据库知识点整理 (持续更新中)
一.修改用户密码 格式(在命令行下输入):mysqladmin -u 用户名 -p旧密码 password 新密码 1. 给root添加密码ab12: mysqladmin -uroot -pass ...
- mysql整理类型_MYSQL数据类型分析整理
1,时间类型 类型 所站空间 datetime 8字节 timestamp 4字节 date 3字节 ye ...
- mysql字段简索引_MySQL优化看这一篇就够了
本文概要 概述 为什么要优化 系统的吞吐量瓶颈往往出现在数据库的访问速度上 随着应用程序的运行,数据库的中的数据会越来越多,处理时间会相应变慢 数据是存放在磁盘上的,读写速度无法和内存相比 如何优化 ...
- mysql ip比较大小_MySQL优化/面试,看这一篇就够了
原文链接:http://www.zhenganwen.top/articles/2018/12/25/1565048860202.html 作者:Anwen~ 链接:https://www.nowco ...
- mysql防止索引崩溃_MySQL优化之避免索引失效的方法
在上一篇文章中,通过分析执行计划的字段说明,大体说了一下索引优化过程中的一些注意点,那么如何才能避免索引失效呢?本篇文章将来讨论这个问题. 避免索引失效的常见方法 1.对于复合索引的使用,应按照索引建 ...
最新文章
- ShardingSphere-Proxy分库分表以及多租户安装使用
- 实际上module(data) 等价于module.forward(data)的原因分析
- .Net 4.0 (2)
- 常见的网页技巧(转)
- ASP.NET 2.2 Preview 1首次支持Java SignalR客户端
- Teleport Ultra 抓包工具
- android游戏模式,注重游戏体验 Android 12提供原生游戏模式
- 深入理解@Autowired注解
- 【HDU 4352】 XHXJ's LIS (数位DP+状态压缩+LIS)
- ubuntu gif 制作工具
- 绝对经典英语学习资料
- 浅谈“从神经网络——迁移学习引发的一系列的思考”
- 现场直击 | 复旦MBA科创青干营开营
- mysql主从怎么确保数据一致_如何保证主从复制数据一致性
- redis mset是否具有原子性
- 个人调节税用计算机,2020税前个人所得税计算器_在线个税计算器_税后工资计算器...
- 分门别类刷leetcode——链表 1
- Tech Blog by Eason!
- 在jsp中使用ECharts制作图表
- 尼尔机械纪元房顶机器人_尼尔机械纪元辅助机器人是什么 辅助机器人解析攻略...