标签:大小 and 添加 方法 作用 root 写锁 表数 计算
一、表碎片产生的原因
因为使用delete删除数据的时候,MySQL并不会把数据文件真实删除,而只是将数据文件的标识位删除,也没有整理数据文件,因此不会彻底释放表空间。换句话说,每当我们从表中删除数据时,这段被删除数据的空间就会被留出来,如果又赶上某段时间内该表进行大量的delete操作,则这部分被删除数据的空间就会越来越大。当有新数据写入时,MySQL会再次利用这些被删除的区域,但也无法彻底占用。
二、碎片的计算方法
mysql> show table status like "%employees%"\G;
*************************** 1. row ***************************
Name: employees
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 299335
Avg_row_length: 50
Data_length: 15220736
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2018-07-17 13:12:08
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.01 sec)
(1)碎片大小=数据总大小-实际表空间文件大小
(2)数据总大小=data_length+index_length=15220736
(3)实际表空间文件大小=rows*avg_rog_length=299335*50=14966750
(4)碎片大小=(15220736-14966750)/1024/1024=253986/1024/1024(M)
三、清除碎片的两方法
(1)alter table table_name engine=innodb
如:
mysql> alter table employees engine=innodb;
Query OK, 0 rows affected (1.33 sec)
Records: 0 Duplicates: 0 Warnings: 0
作用:重新整理一遍全表数据,整理之后的数据连续性好,全表扫描变快,表空间也变小了,节约了磁盘上的空间,清除了碎片。
缺点:需要先给表加个写锁,业务高峰不建议大家使用。推荐使用Percona公司的percona-toolkit工具集pt-online-schema-change,其可以在线整理表结构、收集碎片、给大表添加字段和索引,避免出现锁表导致阻塞读写的操作。MySQL不需要用这个命令,因为可以直接在线"Online DDL";
[root@zstedu bin]# ./pt-online-schema-change --use=root --password=andyxi --host=127.0.0.1 --alter="engine=innodb" D=employees,t=salaries --execute
# A software update is available:
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-07-20T12:38:57 Creating triggers...
2018-07-20T12:38:57 Created triggers OK.
2018-07-20T12:38:57 Copying approximately 2683382 rows...
Copying `employees`.`salaries`: 40% 00:44 remain
Copying `employees`.`salaries`: 80% 00:14 remain
2018-07-20T12:40:18 Copied rows OK.
2018-07-20T12:40:18 Analyzing new table...
2018-07-20T12:40:18 Swapping tables...
2018-07-20T12:40:18 Swapped original and new tables OK.
2018-07-20T12:40:18 Dropping old table...
2018-07-20T12:40:18 Dropped old table `employees`.`_salaries_old` OK.
2018-07-20T12:40:18 Dropping triggers...
2018-07-20T12:40:18 Dropped triggers OK.
Successfully altered `employees`.`salaries`.
(2)备份原表数据,然后删掉,重新导入到新表中(与原表结构一样)
四、表统计信息
mysql> select table_schema,sum(data_length)/1024/1024/1024 as data_length,sum(index_length)/1024/1024/1024 as index_lentgh,sum(data_length+index_length)/1024/1024/1024 as sum_data_index from information_schema.tables where table_schema !=‘information_schema‘ and table_schema !=‘mysql‘ group by table_schema;
+--------------------+----------------+----------------+----------------+
| table_schema | data_length | index_lentgh | sum_data_index |
+--------------------+----------------+----------------+----------------+
| andyhsi | 0.000015258789 | 0.000000000000 | 0.000015258789 |
| andyxi | 0.000030517578 | 0.000000000000 | 0.000030517578 |
| employees | 0.139892578125 | 0.005416870117 | 0.145309448242 |
| otter | 0.000305175781 | 0.000350952148 | 0.000656127930 |
| performance_schema | 0.000000000000 | 0.000000000000 | 0.000000000000 |
| sys | 0.000015258789 | 0.000000000000 | 0.000015258789 |
+--------------------+----------------+----------------+----------------+
6 rows in set (0.07 sec)
标签:大小 and 添加 方法 作用 root 写锁 表数 计算
原文地址:https://www.cnblogs.com/chinaops/p/9352185.html