自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一入職!就遇到MySQL億級大表優(yōu)化....

數(shù)據(jù)庫 MySQL
XX 實(shí)例(一主一從)xxx 告警中每天凌晨在報(bào) SLA 報(bào)警,該報(bào)警的意思是存在一定的主從延遲。(若在此時(shí)發(fā)生主從切換,需要長時(shí)間才可以完成切換,要追延遲來保證主從數(shù)據(jù)的一致性)

前段時(shí)間剛?cè)肼氁患夜荆陀龅搅?MySQL 億級大表優(yōu)化這事!

[[321373]]

圖片來自 Pexels

背景

XX 實(shí)例(一主一從)xxx 告警中每天凌晨在報(bào) SLA 報(bào)警,該報(bào)警的意思是存在一定的主從延遲。(若在此時(shí)發(fā)生主從切換,需要長時(shí)間才可以完成切換,要追延遲來保證主從數(shù)據(jù)的一致性)

XX 實(shí)例的慢查詢數(shù)量最多(執(zhí)行時(shí)間超過 1s 的 SQL 會(huì)被記錄),XX 應(yīng)用那方每天晚上在做刪除一個(gè)月前數(shù)據(jù)的任務(wù)。

分析

使用 pt-query-digest 工具分析最近一周的 mysql-slow.log:

  1. pt-query-digest --since=148h mysql-slow.log | less 

結(jié)果第一部分:

最近一個(gè)星期內(nèi),總共記錄的慢查詢執(zhí)行花費(fèi)時(shí)間為 25403s,最大的慢 SQL 執(zhí)行時(shí)間為 266s,平均每個(gè)慢 SQL 執(zhí)行時(shí)間 5s,平均掃描的行數(shù)為 1766 萬。

結(jié)果第二部分:

select arrival_record 操作記錄的慢查詢數(shù)量最多有 4 萬多次,平均響應(yīng)時(shí)間為 4s,delete arrival_record 記錄了 6 次,平均響應(yīng)時(shí)間 258s。

select xxx_record 語句

select arrival_record 慢查詢語句都類似于如下所示,where 語句中的參數(shù)字段是一樣的,傳入的參數(shù)值不一樣:

  1. select count(*) from arrival_record where product_id=26 and receive_time between '2019-03-25 14:00:00' and '2019-03-25 15:00:00' and receive_spend_ms>=0\G 

select arrival_record 語句在 MySQL 中最多掃描的行數(shù)為 5600 萬、平均掃描的行數(shù)為 172 萬,推斷由于掃描的行數(shù)多導(dǎo)致的執(zhí)行時(shí)間長。

查看執(zhí)行計(jì)劃:

  1. explain select count(*) from arrival_record where product_id=26 and receive_time between '2019-03-25 14:00:00' and '2019-03-25 15:00:00' and receive_spend_ms>=0\G; 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: SIMPLE 
  5. table: arrival_record 
  6. partitions: NULL 
  7. type: ref 
  8. possible_keys: IXFK_arrival_record 
  9. key: IXFK_arrival_record 
  10. key_len: 8 
  11. ref: const 
  12. rows: 32261320 
  13. filtered: 3.70 
  14. Extra: Using index condition; Using where 
  15. 1 row in set, 1 warning (0.00 sec) 

用到了索引 IXFK_arrival_record,但預(yù)計(jì)掃描的行數(shù)很多有 3000 多萬行:

  1. show index from arrival_record; 
  2. +----------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  3. Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
  4. +----------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  5. | arrival_record | 0 | PRIMARY | 1 | id | A | 107990720 | NULL | NULL | | BTREE | | | 
  6. | arrival_record | 1 | IXFK_arrival_record | 1 | product_id | A | 1344 | NULL | NULL | | BTREE | | | 
  7. | arrival_record | 1 | IXFK_arrival_record | 2 | station_no | A | 22161 | NULL | NULL | YES | BTREE | | | 
  8. | arrival_record | 1 | IXFK_arrival_record | 3 | sequence | A | 77233384 | NULL | NULL | | BTREE | | | 
  9. | arrival_record | 1 | IXFK_arrival_record | 4 | receive_time | A | 65854652 | NULL | NULL | YES | BTREE | | | 
  10. | arrival_record | 1 | IXFK_arrival_record | 5 | arrival_time | A | 73861904 | NULL | NULL | YES | BTREE | | | 
  11. +----------------+------------+---------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
  12.  
  13. show create table arrival_record; 
  14. .......... 
  15. arrival_spend_ms bigint(20) DEFAULT NULL
  16. total_spend_ms bigint(20) DEFAULT NULL
  17. PRIMARY KEY (id), 
  18. KEY IXFK_arrival_record (product_id,station_no,sequence,receive_time,arrival_time) USING BTREE, 
  19. CONSTRAINT FK_arrival_record_product FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE NO ACTION ON UPDATE NO ACTION 
  20. ) ENGINE=InnoDB AUTO_INCREMENT=614538979 DEFAULT CHARSET=utf8 COLLATE=utf8_bin | 

①該表總記錄數(shù)約 1 億多條,表上只有一個(gè)復(fù)合索引,product_id 字段基數(shù)很小,選擇性不好。

②傳入的過濾條件:

  1. where product_id=26 and receive_time between '2019-03-25 14:00:00' and '2019-03-25 15:00:00' and receive_spend_ms>=0 

沒有 station_nu 字段,使用不到復(fù)合索引 IXFK_arrival_record 的 product_id,station_no,sequence,receive_time 這幾個(gè)字段。

③根據(jù)最左前綴原則,select arrival_record 只用到了復(fù)合索引 IXFK_arrival_record 的第一個(gè)字段 product_id,而該字段選擇性很差,導(dǎo)致掃描的行數(shù)很多,執(zhí)行時(shí)間長。

④receive_time 字段的基數(shù)大,選擇性好,可對該字段單獨(dú)建立索引,select arrival_record sql 就會(huì)使用到該索引。

現(xiàn)在已經(jīng)知道了在慢查詢中記錄的 select arrival_record where 語句傳入的參數(shù)字段有 product_id,receive_time,receive_spend_ms,還想知道對該表的訪問有沒有通過其他字段來過濾了?

神器 tcpdump 出場的時(shí)候到了,使用 tcpdump 抓包一段時(shí)間對該表的 select 語句:

  1. tcpdump -i bond0 -s 0 -l -w - dst port 3316 | strings | grep select | egrep -i 'arrival_record' >/tmp/select_arri.log 

獲取 select 語句中 from 后面的 where 條件語句:

  1. IFS_OLD=$IFS 
  2. IFS=$'\n' 
  3. for i in `cat /tmp/select_arri.log `;do echo ${i#*'from'}; done | less 
  4. IFS=$IFS_OLD 
  5.  
  6. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=17 and arrivalrec0_.station_no='56742' 
  7. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S7100' 
  8. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4631' 
  9. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S9466' 
  10. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4205' 
  11. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4105' 
  12. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4506' 
  13. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=24 and arrivalrec0_.station_no='V4617' 
  14. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S8356' 
  15. arrival_record arrivalrec0_ where arrivalrec0_.sequence='2019-03-27 08:40' and arrivalrec0_.product_id=22 and arrivalrec0_.station_no='S8356' 

select 該表 where 條件中有 product_id,station_no,sequence 字段,可以使用到復(fù)合索引 IXFK_arrival_record 的前三個(gè)字段。

綜上所示,優(yōu)化方法為:

  • 刪除復(fù)合索引 IXFK_arrival_record
  • 建立復(fù)合索引 idx_sequence_station_no_product_id
  • 建立單獨(dú)索引 indx_receive_time

delete xxx_record 語句

該 delete 操作平均掃描行數(shù)為 1.1 億行,平均執(zhí)行時(shí)間是 262s。

delete 語句如下所示,每次記錄的慢查詢傳入的參數(shù)值不一樣:

  1. delete from arrival_record where receive_time < STR_TO_DATE('2019-02-23''%Y-%m-%d')\G 

執(zhí)行計(jì)劃:

  1. explain select * from arrival_record where receive_time < STR_TO_DATE('2019-02-23''%Y-%m-%d')\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: SIMPLE 
  5. table: arrival_record 
  6. partitions: NULL 
  7. type: ALL 
  8. possible_keys: NULL 
  9. keyNULL 
  10. key_len: NULL 
  11. ref: NULL 
  12. rows: 109501508 
  13. filtered: 33.33 
  14. Extra: Using where 
  15. 1 row in set, 1 warning (0.00 sec) 

該 delete 語句沒有使用索引(沒有合適的索引可用),走的全表掃描,導(dǎo)致執(zhí)行時(shí)間長。

優(yōu)化方法也是:建立單獨(dú)索引 indx_receive_time(receive_time)。

測試

拷貝 arrival_record 表到測試實(shí)例上進(jìn)行刪除重新索引操作。

XX 實(shí)例 arrival_record 表信息:

  1. du -sh /datas/mysql/data/3316/cq_new_cimiss/arrival_record* 
  2. 12K    /datas/mysql/data/3316/cq_new_cimiss/arrival_record.frm 
  3. 48G    /datas/mysql/data/3316/cq_new_cimiss/arrival_record.ibd 
  4.  
  5. select count() from cq_new_cimiss.arrival_record; 
  6. +-----------+ 
  7. count() | 
  8. +-----------+ 
  9. | 112294946 | 
  10. +-----------+ 
  11. 1億多記錄數(shù) 
  12.  
  13. SELECT 
  14. table_name, 
  15. CONCAT(FORMAT(SUM(data_length) / 1024 / 1024,2),'M'AS dbdata_size, 
  16. CONCAT(FORMAT(SUM(index_length) / 1024 / 1024,2),'M'AS dbindex_size, 
  17. CONCAT(FORMAT(SUM(data_length + index_length) / 1024 / 1024 / 1024,2),'G'AS table_size(G), 
  18. AVG_ROW_LENGTH,table_rows,update_time 
  19. FROM 
  20. information_schema.tables 
  21. WHERE table_schema = 'cq_new_cimiss' and table_name='arrival_record'
  22.  
  23. +----------------+-------------+--------------+------------+----------------+------------+---------------------+ 
  24. | table_name | dbdata_size | dbindex_size | table_size(G) | AVG_ROW_LENGTH | table_rows | update_time | 
  25. +----------------+-------------+--------------+------------+----------------+------------+---------------------+ 
  26. | arrival_record | 18,268.02M | 13,868.05M | 31.38G | 175 | 109155053 | 2019-03-26 12:40:17 | 
  27. +----------------+-------------+--------------+------------+----------------+------------+---------------------+ 

磁盤占用空間 48G,MySQL 中該表大小為 31G,存在 17G 左右的碎片,大多由于刪除操作造成的。(記錄被刪除了,空間沒有回收)

備份還原該表到新的實(shí)例中,刪除原來的復(fù)合索引,重新添加索引進(jìn)行測試。

mydumper 并行壓縮備份:

  1. user=root 
  2.  passwd=xxxx 
  3. socket=/datas/mysql/data/3316/mysqld.sock 
  4. db=cq_new_cimiss 
  5. table_name=arrival_record 
  6. backupdir=/datas/dump_$table_name 
  7. mkdir -p $backupdir 
  8.  
  9.   nohup echo `date +%T` && mydumper -u $user -p $passwd -S $socket  -B $db -c  -T $table_name  -o $backupdir  -t 32 -r 2000000 && echo `date +%T` & 

并行壓縮備份所花時(shí)間(52s)和占用空間(1.2G,實(shí)際該表占用磁盤空間為 48G,mydumper 并行壓縮備份壓縮比相當(dāng)高):

  1. Started dump at: 2019-03-26 12:46:04 
  2. ........ 
  3.  
  4. Finished dump at: 2019-03-26 12:46:56 
  5.  
  6. du -sh   /datas/dump_arrival_record/ 
  7. 1.2G    /datas/dump_arrival_record/ 

拷貝 dump 數(shù)據(jù)到測試節(jié)點(diǎn):

  1. scp -rp /datas/dump_arrival_record root@10.230.124.19:/datas 

多線程導(dǎo)入數(shù)據(jù):

  1. time myloader -u root -S /datas/mysql/data/3308/mysqld.sock -P 3308 -p root -B test -d /datas/dump_arrival_record -t 32 
  2.  
  3. real 126m42.885s 
  4. user 1m4.543s 
  5. sys 0m4.267s 

邏輯導(dǎo)入該表后磁盤占用空間:

  1. du -h -d 1 /datas/mysql/data/3308/test/arrival_record.* 
  2. 12K /datas/mysql/data/3308/test/arrival_record.frm 
  3. 30G /datas/mysql/data/3308/test/arrival_record.ibd 
  4. 沒有碎片,和mysql的該表的大小一致 
  5. cp -rp /datas/mysql/data/3308 /datas 

分別使用 online DDL 和 pt-osc 工具來做刪除重建索引操作。

先刪除外鍵,不刪除外鍵,無法刪除復(fù)合索引,外鍵列屬于復(fù)合索引中第一列:

  1. nohup bash /tmp/ddl_index.sh & 
  2. 2019-04-04-10:41:39 begin stop mysqld_3308 
  3. 2019-04-04-10:41:41 begin rm -rf datadir and cp -rp datadir_bak 
  4. 2019-04-04-10:46:53 start mysqld_3308 
  5. 2019-04-04-10:46:59 online ddl begin 
  6. 2019-04-04-11:20:34 onlie ddl stop 
  7. 2019-04-04-11:20:34 begin stop mysqld_3308 
  8. 2019-04-04-11:20:36 begin rm -rf datadir and cp -rp datadir_bak 
  9. 2019-04-04-11:22:48 start mysqld_3308 
  10. 2019-04-04-11:22:53 pt-osc begin 
  11. 2019-04-04-12:19:15 pt-osc stop 

online DDL 花費(fèi)時(shí)間為 34 分鐘,pt-osc 花費(fèi)時(shí)間為 57 分鐘,使用 onlne DDL 時(shí)間約為 pt-osc 工具時(shí)間的一半。

做 DDL 參考:

實(shí)施

由于是一主一從實(shí)例,應(yīng)用是連接的 vip,刪除重建索引采用 online DDL 來做。

停止主從復(fù)制后,先在從實(shí)例上做(不記錄 binlog),主從切換,再在新切換的從實(shí)例上做(不記錄 binlog):

  1. function red_echo () { 
  2.  
  3.         local what="$*" 
  4.         echo -e "$(date +%F-%T)  ${what}" 
  5.  
  6. function check_las_comm(){ 
  7.     if [ "$1" != "0" ];then 
  8.         red_echo "$2" 
  9.         echo "exit 1" 
  10.         exit 1 
  11.     fi 
  12.  
  13. red_echo "stop slave" 
  14. mysql -uroot -p$passwd --socket=/datas/mysql/data/${port}/mysqld.sock -e"stop slave" 
  15. check_las_comm "$?" "stop slave failed" 
  16.  
  17. red_echo "online ddl begin" 
  18.  mysql -uroot -p$passwd --socket=/datas/mysql/data/${port}/mysqld.sock -e"set sql_log_bin=0;select now() as  ddl_start;ALTER TABLE $db_.\`${table_name}\` DROP FOREIGN KEY FK_arrival_record_product,drop index IXFK_arrival_record,add index idx_product_id_sequence_station_no(product_id,sequence,station_no),add index idx_receive_time(receive_time);select now() as ddl_stop" >>${log_file} 2>& 1 
  19.  red_echo "onlie ddl stop" 
  20.  red_echo "add foreign key" 
  21.  mysql -uroot -p$passwd --socket=/datas/mysql/data/${port}/mysqld.sock -e"set sql_log_bin=0;ALTER TABLE $db_.${table_name} ADD CONSTRAINT _FK_${table_name}_product FOREIGN KEY (product_id) REFERENCES cq_new_cimiss.product (id) ON DELETE NO ACTION ON UPDATE NO ACTION;" >>${log_file} 2>& 1 
  22.  check_las_comm "$?" "add foreign key error" 
  23.  red_echo "add foreign key stop" 
  24.  
  25. red_echo "start slave" 
  26. mysql -uroot -p$passwd --socket=/datas/mysql/data/${port}/mysqld.sock -e"start slave" 
  27. check_las_comm "$?" "start slave failed" 

執(zhí)行時(shí)間:

  1. 2019-04-08-11:17:36 stop slave 
  2. mysql: [Warning] Using a password on the command line interface can be insecure. 
  3. ddl_start 
  4. 2019-04-08 11:17:36 
  5. ddl_stop 
  6. 2019-04-08 11:45:13 
  7. 2019-04-08-11:45:13 onlie ddl stop 
  8. 2019-04-08-11:45:13 add foreign key 
  9. mysql: [Warning] Using a password on the command line interface can be insecure. 
  10. 2019-04-08-12:33:48 add foreign key stop 
  11. 2019-04-08-12:33:48 start slave 

刪除重建索引花費(fèi)時(shí)間為 28 分鐘,添加外鍵約束時(shí)間為 48 分鐘。

再次查看 delete 和 select 語句的執(zhí)行計(jì)劃:

  1. explain select count(*) from arrival_record where receive_time < STR_TO_DATE('2019-03-10''%Y-%m-%d')\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: SIMPLE 
  5. table: arrival_record 
  6. partitions: NULL 
  7. type: range 
  8. possible_keys: idx_receive_time 
  9. key: idx_receive_time 
  10. key_len: 6 
  11. ref: NULL 
  12. rows: 7540948 
  13. filtered: 100.00 
  14. Extra: Using where; Using index 
  15.  
  16. explain select count(*) from arrival_record where product_id=26 and receive_time between '2019-03-25 14:00:00' and '2019-03-25 15:00:00' and receive_spend_ms>=0\G; 
  17. *************************** 1. row *************************** 
  18. id: 1 
  19. select_type: SIMPLE 
  20. table: arrival_record 
  21. partitions: NULL 
  22. type: range 
  23. possible_keys: idx_product_id_sequence_station_no,idx_receive_time 
  24. key: idx_receive_time 
  25. key_len: 6 
  26. ref: NULL 
  27. rows: 291448 
  28. filtered: 16.66 
  29. Extra: Using index condition; Using where 

都使用到了 idx_receive_time 索引,掃描的行數(shù)大大降低。

索引優(yōu)化后

delete 還是花費(fèi)了 77s 時(shí)間:

  1. delete from arrival_record where receive_time < STR_TO_DATE('2019-03-10''%Y-%m-%d')\G 

delete 語句通過 receive_time 的索引刪除 300 多萬的記錄花費(fèi) 77s 時(shí)間。

delete 大表優(yōu)化為小批量刪除

應(yīng)用端已優(yōu)化成每次刪除 10 分鐘的數(shù)據(jù)(每次執(zhí)行時(shí)間 1s 左右),xxx 中沒在出現(xiàn) SLA(主從延遲告警):

另一個(gè)方法是通過主鍵的順序每次刪除 20000 條記錄:

  1. #得到滿足時(shí)間條件的最大主鍵ID 
  2. #通過按照主鍵的順序去 順序掃描小批量刪除數(shù)據(jù) 
  3. #先執(zhí)行一次以下語句 
  4.  SELECT MAX(id) INTO @need_delete_max_id FROM `arrival_record` WHERE receive_time<'2019-03-01' ; 
  5.  DELETE FROM arrival_record WHERE id<@need_delete_max_id LIMIT 20000; 
  6.  select ROW_COUNT();  #返回20000 
  7.  
  8.  
  9. #執(zhí)行小批量delete后會(huì)返回row_count(), 刪除的行數(shù) 
  10. #程序判斷返回的row_count()是否為0,不為0執(zhí)行以下循環(huán),為0退出循環(huán),刪除操作完成 
  11.  DELETE FROM arrival_record WHERE id<@need_delete_max_id LIMIT 20000; 
  12.  select ROW_COUNT(); 
  13. #程序睡眠0.5s 

總結(jié)

表數(shù)據(jù)量太大時(shí),除了關(guān)注訪問該表的響應(yīng)時(shí)間外,還要關(guān)注對該表的維護(hù)成本(如做 DDL 表更時(shí)間太長,delete 歷史數(shù)據(jù))。

對大表進(jìn)行 DDL 操作時(shí),要考慮表的實(shí)際情況(如對該表的并發(fā)表,是否有外鍵)來選擇合適的 DDL 變更方式。

對大數(shù)據(jù)量表進(jìn)行 delete,用小批量刪除的方式,減少對主實(shí)例的壓力和主從延遲。

 

責(zé)任編輯:武曉燕 來源: 博客園
相關(guān)推薦

2020-10-21 11:20:42

MySQL數(shù)據(jù)庫Bug

2021-05-17 08:11:44

MySQL數(shù)據(jù)庫索引

2020-02-11 08:02:26

千萬級大表優(yōu)化

2021-06-29 08:12:22

MySQL數(shù)據(jù)分頁數(shù)據(jù)庫

2024-11-05 11:14:05

2023-07-06 09:01:33

2024-07-24 20:01:03

2016-08-04 13:19:06

MySQL數(shù)據(jù)庫大優(yōu)化

2024-08-06 10:02:42

2025-03-31 01:55:00

2021-03-11 10:55:41

MySQL數(shù)據(jù)庫索引

2018-07-26 14:50:00

數(shù)據(jù)庫MySQL大表優(yōu)化

2020-03-27 15:40:10

MySQL索引數(shù)據(jù)庫

2020-11-17 08:08:34

分庫分表

2021-01-25 18:28:47

Python 代碼特斯拉

2021-07-19 08:41:49

藍(lán)屏用戶Bug

2011-12-14 20:01:39

蘋果

2021-11-26 09:30:11

華為開發(fā)者技術(shù)

2020-11-03 07:48:47

當(dāng)AI入職FBI

2019-08-09 17:53:41

戴爾
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號