主从复制中定位回放慢涉及的表

诗林  论坛元老 | 2025-2-17 15:32:37 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1339|帖子 1339|积分 4017

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
主从复制中定位回放慢涉及的表

一、前提

天下千奇百怪,每个人都有自己独立的思想,有些事变即使你附耳告知,也可能如风般吹过,进而消失,为了性能为了不延迟,表要加索引嘛,然而在某业务场景,业务表数千张,无索引的表几百张,这些表都是上百万的数据。
二、征象

在 GreatSQL 主从架构中,某天在体系资源富足的环境下,主从忽然延迟,而且持续增长,我们通过SHOW PROCESSLIST 和 SHOW SLAVE STATUS 观察是由于回放 DELETE 事件造成的,但是 GTID`` 在不断地增长,不外增长的非常缓慢,但是平时的时间是没有云云缓慢的,我们该如何快速的定位这些回放缓慢的 GTID 的涉及表呢,接下来就在测试环境演示下如何定位。
三、模拟测试

1、主节点创建库
  1. greatsql> CREATE DATABASE fcmark;
  2. Query OK, 1 row affected (0.03 sec)
复制代码
2、准备数据
  1. $ sysbench --db-driver=mysql --mysql-host=192.168.139.230 --mysql-port=3307 --mysql-user=greatsql --mysql-password=greatsql@2024 --mysql-db=fcmark --table_size=10000000 --tables=5 oltp_write_only  prepare
  2. sysbench 1.1.0-df89d34 (using bundled LuaJIT 2.1.0-beta3)
  3. Creating table 'sbtest1'...
  4. Inserting 10000000 records into 'sbtest1'
  5. Creating a secondary index on 'sbtest1'...
  6. Creating table 'sbtest2'...
  7. Inserting 10000000 records into 'sbtest2'
  8. Creating a secondary index on 'sbtest2'...
  9. Creating table 'sbtest3'...
  10. Inserting 10000000 records into 'sbtest3'
  11. Creating a secondary index on 'sbtest3'...
  12. Creating table 'sbtest4'...
  13. Inserting 10000000 records into 'sbtest4'
  14. Creating a secondary index on 'sbtest4'...
  15. Creating table 'sbtest5'...
  16. Inserting 10000000 records into 'sbtest5'
  17. Creating a secondary index on 'sbtest5'...
复制代码
3、主节点修改表布局,将表修改为无索引表
  1. greatsql> ALTER TABLE sbtest1 MODIFY id int not null;
  2. Query OK, 10000000 rows affected (3 min 2.62 sec)
  3. Records: 10000000  Duplicates: 0  Warnings: 0
  4. greatsql> ALTER TABLE sbtest1 DROP key `k_1`;
  5. Query OK, 0 rows affected (0.15 sec)
  6. Records: 0  Duplicates: 0  Warnings: 0
  7. greatsql> ALTER TABLE sbtest1 DROP PRIMARY KEY;
  8. Query OK, 10000000 rows affected (1 min 50.19 sec)
  9. Records: 10000000  Duplicates: 0  Warnings: 0
  10. greatsql> ALTER TABLE sbtest2 MODIFY id int not null;
  11. Query OK, 10000000 rows affected (3 min 2.62 sec)
  12. Records: 10000000  Duplicates: 0  Warnings: 0
  13. greatsql> ALTER TABLE sbtest2 DROP key `k_2`;
  14. Query OK, 0 rows affected (0.15 sec)
  15. Records: 0  Duplicates: 0  Warnings: 0
  16. greatsql> ALTER TABLE sbtest2 DROP  PRIMARY KEY;
  17. Query OK, 10000000 rows affected (1 min 50.19 sec)
  18. Records: 10000000  Duplicates: 0  Warnings: 0
  19. greatsql> ALTER TABLE sbtest3 MODIFY id int not null;
  20. Query OK, 10000000 rows affected (3 min 2.72 sec)
  21. Records: 10000000  Duplicates: 0  Warnings: 0
  22. greatsql> ALTER TABLE sbtest3 DROP key `k_3`;
  23. Query OK, 0 rows affected (0.15 sec)
  24. Records: 0  Duplicates: 0  Warnings: 0
  25. greatsql> ALTER TABLE sbtest3 DROP PRIMARY KEY;
  26. Query OK, 10000000 rows affected (1 min 52.19 sec)
  27. Records: 10000000  Duplicates: 0  Warnings: 0
  28. greatsql> ALTER TABLE sbtest4 MODIFY id int not null;
  29. Query OK, 10000000 rows affected (3 min 3.64 sec)
  30. Records: 10000000  Duplicates: 0  Warnings: 0
  31. greatsql> ALTER TABLE sbtest4 DROP key `k_4`;
  32. Query OK, 0 rows affected (0.15 sec)
  33. Records: 0  Duplicates: 0  Warnings: 0
  34. greatsql> ALTER TABLE sbtest4 DROP PRIMARY KEY;
  35. Query OK, 10000000 rows affected (1 min 53.19 sec)
  36. Records: 10000000  Duplicates: 0  Warnings: 0
  37. greatsql> ALTER TABLE sbtest5 MODIFY id int not null;
  38. Query OK, 10000000 rows affected (3 min 5.62 sec)
  39. Records: 10000000  Duplicates: 0  Warnings: 0
  40. greatsql> ALTER TABLE sbtest5 DROP key `k_5`;
  41. Query OK, 0 rows affected (0.15 sec)
  42. Records: 0  Duplicates: 0  Warnings: 0
  43. greatsql> ALTER TABLE sbtest5 DROP PRIMARY KEY;
  44. Query OK, 10000000 rows affected (1 min 51 sec)
  45. Records: 10000000  Duplicates: 0  Warnings: 0
复制代码
4、主库操作数据模拟延迟
  1. greatsql> DELETE FROM sbtest1 WHERE k<200;
  2. Query OK, 214 rows affected (41.14 sec)
  3. greatsql> DELETE FROM sbtest2 WHERE k<200;
  4. Query OK, 193 rows affected (41.58 sec)
  5. greatsql> DELETE FROM sbtest3 WHERE k<200;
  6. Query OK, 177 rows affected (40.00 sec)
  7. greatsql> DELETE FROM sbtest4 WHERE k<200;
  8. Query OK, 184 rows affected (39.38 sec)
  9. greatsql> DELETE FROM sbtest5 WHERE k<200;
  10. Query OK, 212 rows affected (37.56 sec)
  11. greatsql> DELETE FROM sbtest1 WHERE k<400;
  12. Query OK, 182 rows affected (39.09 sec)
  13. greatsql> DELETE FROM sbtest2 WHERE k<400;
  14. Query OK, 193 rows affected (38.21 sec)
  15. greatsql> DELETE FROM sbtest3 WHERE k<400;
  16. Query OK, 215 rows affected (34.45 sec)
  17. greatsql> DELETE FROM sbtest4 WHERE k<400;
  18. Query OK, 219 rows affected (37.45 sec)
  19. greatsql> DELETE FROM sbtest5 WHERE k<400;
  20. Query OK, 224 rows affected (34.63 sec)
  21. greatsql> DELETE FROM sbtest1 WHERE k<600;
  22. Query OK, 185 rows affected (34.11 sec)
  23. greatsql> DELETE FROM sbtest2 WHERE k<600;
  24. Query OK, 245 rows affected (35.99 sec)
  25. greatsql> DELETE FROM sbtest3 WHERE k<600;
  26. Query OK, 173 rows affected (36.08 sec)
  27. greatsql> DELETE FROM sbtest4 WHERE k<600;
  28. Query OK, 230 rows affected (36.18 sec)
  29. greatsql> DELETE FROM sbtest5 WHERE k<600;
  30. Query OK, 186 rows affected (38.00 sec)
复制代码
6、分析数据

主节点操作,根据从节点观察的信息,在主节点上观察 binlog 信息,回放内容是关于 fcmark.sbtest4`` 表的多个DELETE事件,观察该表布局是无索引,并且数据量接近 1万万
  1. greatsql> SHOW PROCESSLIST;
  2. +--------+-----------------+----------------------+--------+---------+---------+---------------------------------------------+------------------+------------+-----------+---------------+
  3. | Id     | User            | Host                 | db     | Command | Time    | State                                       | Info             | Time_ms    | Rows_sent | Rows_examined |
  4. +--------+-----------------+----------------------+--------+---------+---------+---------------------------------------------+------------------+------------+-----------+---------------+
  5. |      5 | event_scheduler | localhost            | NULL   | Daemon  | 2356467 | Waiting on empty queue                      | NULL             | 2356467469 |         0 |             0 |
  6. |     17 | system user     | connecting host      | NULL   | Connect | 2356460 | Waiting for source to send event            | NULL             | 2356459985 |         0 |             0 |
  7. |     18 | system user     |                      | NULL   | Query   |      99 | Waiting for dependent transaction to commit | NULL             |      99081 |         0 |             0 |
  8. |     19 | system user     |                      | fcmark | Query   |     697 | Applying batch of row changes (delete)      | NULL             |      32977 |         0 |             0 |
  9. |     20 | system user     |                      | NULL   | Query   |    3244 | Waiting for an event from Coordinator       | NULL             |    3242579 |         0 |             0 |
  10. |     21 | system user     |                      | NULL   | Connect | 2356460 | Waiting for an event from Coordinator       | NULL             | 2356459981 |         0 |             0 |
  11. |     22 | system user     |                      | NULL   | Connect | 2356460 | Waiting for an event from Coordinator       | NULL             | 2356459980 |         0 |             0 |
  12. | 540493 | greatsql         | 172.17.136.93:34298  | NULL   | Sleep   |      83 |                                             | NULL             |      83365 |         0 |             0 |
  13. | 540593 | greatsql         | 172.17.139.230:51514 | NULL   | Query   |       0 | init                                        | show processlist |          0 |         0 |             0 |
  14. | 540660 | greatsql         | 172.17.136.93:46212  | NULL   | Sleep   |     383 |                                             | NULL             |     383304 |         0 |             0 |
  15. | 540730 | greatsql         | 172.17.136.93:51128  | NULL   | Sleep   |     143 |                                             | NULL             |     143319 |         0 |             0 |
  16. | 540758 | greatsql         | 172.17.136.93:53112  | NULL   | Sleep   |      23 |                                             | NULL             |      23205 |         0 |             0 |
  17. | 540763 | greatsql         | 172.17.139.230:52204 | NULL   | Sleep   |       1 |                                             | NULL             |       1246 |         0 |             0 |
  18. +--------+-----------------+----------------------+--------+---------+---------+---------------------------------------------+------------------+------------+-----------+---------------+
  19. 13 rows in set (0.00 sec)
  20. greatsql> SHOW SLAVE STATUS \G
  21. *************************** 1. row ***************************
  22.                Slave_IO_State: Waiting for source to send event
  23.                   Master_Host: 172.17.139.230
  24.                   Master_User: greatsql
  25.                   Master_Port: 3307
  26.                 Connect_Retry: 60
  27.               Master_Log_File: binlog.000064
  28.           Read_Master_Log_Pos: 714784459
  29.                Relay_Log_File: relaylog.000189
  30.                 Relay_Log_Pos: 714314261
  31.         Relay_Master_Log_File: binlog.000064
  32.              Slave_IO_Running: Yes
  33.             Slave_SQL_Running: Yes
  34.               Replicate_Do_DB:
  35.           Replicate_Ignore_DB:
  36.            Replicate_Do_Table:
  37.        Replicate_Ignore_Table:
  38.       Replicate_Wild_Do_Table:
  39.   Replicate_Wild_Ignore_Table:
  40.                    Last_Errno: 0
  41.                    Last_Error:
  42.                  Skip_Counter: 0
  43.           Exec_Master_Log_Pos: 714314051
  44.               Relay_Log_Space: 714784956
  45.               Until_Condition: None
  46.                Until_Log_File:
  47.                 Until_Log_Pos: 0
  48.            Master_SSL_Allowed: No
  49.            Master_SSL_CA_File:
  50.            Master_SSL_CA_Path:
  51.               Master_SSL_Cert:
  52.             Master_SSL_Cipher:
  53.                Master_SSL_Key:
  54.         Seconds_Behind_Master: 700
  55. Master_SSL_Verify_Server_Cert: No
  56.                 Last_IO_Errno: 0
  57.                 Last_IO_Error:
  58.                Last_SQL_Errno: 0
  59.                Last_SQL_Error:
  60.   Replicate_Ignore_Server_Ids:
  61.              Master_Server_Id: 1000403307
  62.                   Master_UUID: 1d0963e9-85d9-11ef-80e1-00163e28e06a
  63.              Master_Info_File: mysql.slave_master_info
  64.                     SQL_Delay: 0
  65.           SQL_Remaining_Delay: NULL
  66.       Slave_SQL_Running_State: Waiting for dependent transaction to commit
  67.            Master_Retry_Count: 86400
  68.                   Master_Bind:
  69.       Last_IO_Error_Timestamp:
  70.      Last_SQL_Error_Timestamp:
  71.                Master_SSL_Crl:
  72.            Master_SSL_Crlpath:
  73.            Retrieved_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-133182
  74.             Executed_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-133170
  75.                 Auto_Position: 1
  76.          Replicate_Rewrite_DB:
  77.                  Channel_Name:
  78.            Master_TLS_Version:
  79.        Master_public_key_path:
  80.         Get_master_public_key: 0
  81.             Network_Namespace:
  82. 1 row in set, 1 warning (0.00 sec)
  83. greatsql> SHOW SLAVE STATUS \G
  84. *************************** 1. row ***************************
  85.                Slave_IO_State: Waiting for source to send event
  86.                   Master_Host: 172.17.139.230
  87.                   Master_User: greatsql
  88.                   Master_Port: 3307
  89.                 Connect_Retry: 60
  90.               Master_Log_File: binlog.000064
  91.           Read_Master_Log_Pos: 714784459
  92.                Relay_Log_File: relaylog.000189
  93.                 Relay_Log_Pos: 714314261
  94.         Relay_Master_Log_File: binlog.000064
  95.              Slave_IO_Running: Yes
  96.             Slave_SQL_Running: Yes
  97.               Replicate_Do_DB:
  98.           Replicate_Ignore_DB:
  99.            Replicate_Do_Table:
  100.        Replicate_Ignore_Table:
  101.       Replicate_Wild_Do_Table:
  102.   Replicate_Wild_Ignore_Table:
  103.                    Last_Errno: 0
  104.                    Last_Error:
  105.                  Skip_Counter: 0
  106.           Exec_Master_Log_Pos: 714314051
  107.               Relay_Log_Space: 714784956
  108.               Until_Condition: None
  109.                Until_Log_File:
  110.                 Until_Log_Pos: 0
  111.            Master_SSL_Allowed: No
  112.            Master_SSL_CA_File:
  113.            Master_SSL_CA_Path:
  114.               Master_SSL_Cert:
  115.             Master_SSL_Cipher:
  116.                Master_SSL_Key:
  117.         Seconds_Behind_Master: 704
  118. Master_SSL_Verify_Server_Cert: No
  119.                 Last_IO_Errno: 0
  120.                 Last_IO_Error:
  121.                Last_SQL_Errno: 0
  122.                Last_SQL_Error:
  123.   Replicate_Ignore_Server_Ids:
  124.              Master_Server_Id: 1000403307
  125.                   Master_UUID: 1d0963e9-85d9-11ef-80e1-00163e28e06a
  126.              Master_Info_File: mysql.slave_master_info
  127.                     SQL_Delay: 0
  128.           SQL_Remaining_Delay: NULL
  129.       Slave_SQL_Running_State: Waiting for dependent transaction to commit
  130.            Master_Retry_Count: 86400
  131.                   Master_Bind:
  132.       Last_IO_Error_Timestamp:
  133.      Last_SQL_Error_Timestamp:
  134.                Master_SSL_Crl:
  135.            Master_SSL_Crlpath:
  136.            Retrieved_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-133182
  137.             Executed_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-133170
  138.                 Auto_Position: 1
  139.          Replicate_Rewrite_DB:
  140.                  Channel_Name:
  141.            Master_TLS_Version:
  142.        Master_public_key_path:
  143.         Get_master_public_key: 0
  144.             Network_Namespace:
  145. 1 row in set, 1 warning (0.00 sec)
复制代码
四、总结

利用SHOW PROCESSLIST和SHOW SLAVE STATUS观察是数据库正在回放数据,找到正在回放的主库的binlog位点以及正在回放的GTID,在主库上利用 SHOW BINLOG EVENTS IN binlog文件 FROM 位点 LIMIT 步长,来找到对应的表布局和事件类型,进而观察表布局,该表无索引且数据量大,以是导致从库回放的时间比较慢,至于处理办法,可以暂时KILL 掉回放线程,然后加上索引,在重启回放线程,详细细节就不详细展开了。

Enjoy GreatSQL
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

诗林

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表