1. 程式人生 > >碎片計算方法及整理過程

碎片計算方法及整理過程

upd mysql 表結構 col root 過程 free rem -a

一、表碎片產生的原因

因為使用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)

碎片計算方法及整理過程