加快无索引表引起的主从延长数据回放

打印 上一主题 下一主题

主题 876|帖子 876|积分 2628

加快无索引表引起的主从延长数据回放

一、场景

由于某些缘故原由,客户现场存在一张 8千万 的大表,而且该表上无任何索引(也无主键),平时该表上 UPDATE 或 DELETE 只操作几条数据。忽然有一天业务进行了某种操作,DELETE 2万 条数据,悲剧发生了,当在主库上执行了之后,传到从库上之后一直回放,当时评估了下可能会回放10天,后来在经过业务同意之后,对表进行操作,用于加快回放日志,处理该问题。
二、处理思路

停掉复制线程,关闭 Binlog 日志记载,添加索引,重启复制线程。
三、复现步骤

1、准备数据:

创建库:
  1. greatsql> CREATE DATABASE qj;
  2. Query OK, 1 row affected (0.01 sec)
复制代码
准备数据,准备一张8千万数据的表:
  1. $ sysbench --db-driver=mysql --mysql-host=192.168.139.230 --mysql-port=3307 --mysql-user=greatsql --mysql-password=GreatSQL@2024 --mysql-db=qj --table_size=80000000 --tables=1 oltp_write_only  prepare
  2. sysbench 1.1.0-df89d34 (using bundled LuaJIT 2.1.0-beta3)
  3. Creating table 'sbtest1'...
  4. Inserting 80000000 records into 'sbtest1'
  5. Creating a secondary index on 'sbtest1'...
复制代码
主节点修改表布局,将表的索引去掉
  1. greatsql> USE qj
  2. Database changed
  3. greatsqll> ALTER TABLE sbtest1 MODIFY id int not null;
  4. Query OK, 80000000 rows affected (36 min 29.13 sec)
  5. Records: 80000000  Duplicates: 0  Warnings: 0
  6. greatsql> ALTER TABLE sbtest1 DROP key `k_1`;
  7. Query OK, 0 rows affected (0.96 sec)
  8. Records: 0  Duplicates: 0  Warnings: 0
  9. greatsql> ALTER TABLE sbtest1 DROP PRIMARY KEY;
  10. Query OK, 80000000 rows affected (14 min 29.31 sec)
  11. Records: 80000000  Duplicates: 0  Warnings: 0
复制代码
2、模仿延长:

01、主节点:
模仿删除数据
  1. greatsql> DELETE FROM qj.sbtest1 WHERE  k<20000;
  2. Query OK, 19894 rows affected (4 min 20.40 sec)
复制代码
03、通过操作从节点加快日志回放
找到回放线程,KILL掉回放线程,关闭当前会话binlog日志记载,添加索引,重新启动复制回放线程
  1. greatsql> SELECT sleep(300);
  2. +------------+
  3. | sleep(300) |
  4. +------------+
  5. |          0 |
  6. +------------+
  7. 1 row in set (5 min 0.01 sec)
  8. greatsql> SHOW SLAVE STATUS \G
  9. *************************** 1. row ***************************
  10.                Slave_IO_State: Waiting for source to send event
  11.                   Master_Host: 172.17.139.230
  12.                   Master_User: greatsql
  13.                   Master_Port: 3307
  14.                 Connect_Retry: 60
  15.               Master_Log_File: binlog.000078
  16.           Read_Master_Log_Pos: 953750186
  17.                Relay_Log_File: relaylog.000231
  18.                 Relay_Log_Pos: 949954084
  19.         Relay_Master_Log_File: binlog.000078
  20.              Slave_IO_Running: Yes
  21.             Slave_SQL_Running: Yes
  22.               Replicate_Do_DB:
  23.           Replicate_Ignore_DB:
  24.            Replicate_Do_Table:
  25.        Replicate_Ignore_Table:
  26.       Replicate_Wild_Do_Table:
  27.   Replicate_Wild_Ignore_Table:
  28.                    Last_Errno: 0
  29.                    Last_Error:
  30.                  Skip_Counter: 0
  31.           Exec_Master_Log_Pos: 949953874
  32.               Relay_Log_Space: 953750683
  33.               Until_Condition: None
  34.                Until_Log_File:
  35.                 Until_Log_Pos: 0
  36.            Master_SSL_Allowed: No
  37.            Master_SSL_CA_File:
  38.            Master_SSL_CA_Path:
  39.               Master_SSL_Cert:
  40.             Master_SSL_Cipher:
  41.                Master_SSL_Key:
  42.         Seconds_Behind_Master: 1345
  43. Master_SSL_Verify_Server_Cert: No
  44.                 Last_IO_Errno: 0
  45.                 Last_IO_Error:
  46.                Last_SQL_Errno: 0
  47.                Last_SQL_Error:
  48.   Replicate_Ignore_Server_Ids:
  49.              Master_Server_Id: 1000403307
  50.                   Master_UUID: 1d0963e9-85d9-11ef-80e1-00163e28e06a
  51.              Master_Info_File: mysql.slave_master_info
  52.                     SQL_Delay: 0
  53.           SQL_Remaining_Delay: NULL
  54.       Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
  55.            Master_Retry_Count: 86400
  56.                   Master_Bind:
  57.       Last_IO_Error_Timestamp:
  58.      Last_SQL_Error_Timestamp:
  59.                Master_SSL_Crl:
  60.            Master_SSL_Crlpath:
  61.            Retrieved_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-163235
  62.             Executed_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-163234
  63.                 Auto_Position: 1
  64.          Replicate_Rewrite_DB:
  65.                  Channel_Name:
  66.            Master_TLS_Version:
  67.        Master_public_key_path:
  68.         Get_master_public_key: 0
  69.             Network_Namespace:
  70. 1 row in set, 1 warning (0.00 sec)
  71. greatsql> SHOW GLOBAL  VARIABLES LIKE 'read_only';
  72. +---------------+-------+
  73. | Variable_name | Value |
  74. +---------------+-------+
  75. | read_only     | OFF   |
  76. +---------------+-------+
  77. 1 row in set (0.01 sec)
复制代码
四、思索

01、如何保持主从划一性
02、正在回放数据时,STOP SLAVE 是否会有问题
03、如果主从布局作为数据节点,上层还有计算节点该如何保持元数据的划一性即计算节点记载的表的布局和数据节点的表布局要划一
关于以上三个问题我的处理方案如下:
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  | 2369454 | Waiting on empty queue                                   | NULL             | 2369454039 |         0 |             0 |
  6. |     17 | system user     | connecting host      | NULL | Connect | 2369447 | Waiting for source to send event                         | NULL             | 2369446555 |         0 |             0 |
  7. | 543448 | greatsql         | 172.17.136.93:48298  | NULL | Sleep   |     110 |                                                          | NULL             |     109832 |         0 |             0 |
  8. | 543500 | greatsql         | 172.17.139.230:35198 | NULL | Sleep   |     586 |                                                          | NULL             |     585885 |         0 |             0 |
  9. | 543588 | greatsql         | 172.17.136.93:57948  | NULL | Sleep   |      50 |                                                          | NULL             |      49878 |         0 |             0 |
  10. | 543663 | greatsql         | 172.17.139.230:35726 | NULL | Query   |       0 | init                                                     | show processlist |          0 |         0 |             0 |
  11. | 543666 | greatsql         | 172.17.136.93:58908  | NULL | Sleep   |     290 |                                                          | NULL             |     289861 |         0 |             0 |
  12. | 543708 | system user     |                      | NULL | Query   |     477 | Replica has read all relay log; waiting for more updates | NULL             |     476520 |         0 |             0 |
  13. | 543709 | system user     |                      | qj   | Query   |    1383 | Applying batch of row changes (delete)                   | NULL             |      26102 |         0 |             0 |
  14. | 543710 | system user     |                      | NULL | Connect |     477 | Waiting for an event from Coordinator                    | NULL             |     476570 |         0 |             0 |
  15. | 543711 | system user     |                      | NULL | Connect |     477 | Waiting for an event from Coordinator                    | NULL             |     476569 |         0 |             0 |
  16. | 543712 | system user     |                      | NULL | Connect |     477 | Waiting for an event from Coordinator                    | NULL             |     476568 |         0 |             0 |
  17. | 543714 | greatsql         | 172.17.136.93:33582  | NULL | Sleep   |     470 |                                                          | NULL             |     469798 |         0 |             0 |
  18. +--------+-----------------+----------------------+------+---------+---------+----------------------------------------------------------+------------------+------------+-----------+---------------+
  19. 13 rows in set (0.00 sec)
  20. greatsql> KILL 543709;
  21. Query OK, 0 rows affected (0.00 sec)
  22. greatsql> SHOW PROCESSLIST;
  23. +--------+-----------------+----------------------+------+---------+---------+----------------------------------+------------------+------------+-----------+---------------+
  24. | Id     | User            | Host                 | db   | Command | Time    | State                            | Info             | Time_ms    | Rows_sent | Rows_examined |
  25. +--------+-----------------+----------------------+------+---------+---------+----------------------------------+------------------+------------+-----------+---------------+
  26. |      5 | event_scheduler | localhost            | NULL | Daemon  | 2369473 | Waiting on empty queue           | NULL             | 2369473241 |         0 |             0 |
  27. |     17 | system user     | connecting host      | NULL | Connect | 2369466 | Waiting for source to send event | NULL             | 2369465757 |         0 |             0 |
  28. | 543448 | greatsql         | 172.17.136.93:48298  | NULL | Sleep   |     129 |                                  | NULL             |     129034 |         0 |             0 |
  29. | 543500 | greatsql         | 172.17.139.230:35198 | NULL | Sleep   |     605 |                                  | NULL             |     605087 |         0 |             0 |
  30. | 543588 | greatsql         | 172.17.136.93:57948  | NULL | Sleep   |       9 |                                  | NULL             |       9132 |         0 |             0 |
  31. | 543663 | greatsql         | 172.17.139.230:35726 | NULL | Query   |       0 | init                             | show processlist |          0 |         0 |             0 |
  32. | 543666 | greatsql         | 172.17.136.93:58908  | NULL | Sleep   |     309 |                                  | NULL             |     309064 |         0 |             0 |
  33. | 543714 | greatsql         | 172.17.136.93:33582  | NULL | Sleep   |     489 |                                  | NULL             |     489000 |         0 |             0 |
  34. +--------+-----------------+----------------------+------+---------+---------+----------------------------------+------------------+------------+-----------+---------------+
  35. 8 rows in set (0.00 sec)
  36. greatsql> SHOW SLAVE STATUS \G
  37. *************************** 1. row ***************************
  38.                Slave_IO_State: Waiting for source to send event
  39.                   Master_Host: 172.17.139.230
  40.                   Master_User: greatsql
  41.                   Master_Port: 3307
  42.                 Connect_Retry: 60
  43.               Master_Log_File: binlog.000078
  44.           Read_Master_Log_Pos: 953750186
  45.                Relay_Log_File: relaylog.000231
  46.                 Relay_Log_Pos: 949954084
  47.         Relay_Master_Log_File: binlog.000078
  48.              Slave_IO_Running: Yes
  49.             Slave_SQL_Running: No
  50.               Replicate_Do_DB:
  51.           Replicate_Ignore_DB:
  52.            Replicate_Do_Table:
  53.        Replicate_Ignore_Table:
  54.       Replicate_Wild_Do_Table:
  55.   Replicate_Wild_Ignore_Table:
  56.                    Last_Errno: 1317
  57.                    Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction '1d0963e9-85d9-11ef-80e1-00163e28e06a:163235' at master log binlog.000078, end_log_pos 950241265. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
  58.                  Skip_Counter: 0
  59.           Exec_Master_Log_Pos: 949953874
  60.               Relay_Log_Space: 953750683
  61.               Until_Condition: None
  62.                Until_Log_File:
  63.                 Until_Log_Pos: 0
  64.            Master_SSL_Allowed: No
  65.            Master_SSL_CA_File:
  66.            Master_SSL_CA_Path:
  67.               Master_SSL_Cert:
  68.             Master_SSL_Cipher:
  69.                Master_SSL_Key:
  70.         Seconds_Behind_Master: NULL
  71. Master_SSL_Verify_Server_Cert: No
  72.                 Last_IO_Errno: 0
  73.                 Last_IO_Error:
  74.                Last_SQL_Errno: 1317
  75.                Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction '1d0963e9-85d9-11ef-80e1-00163e28e06a:163235' at master log binlog.000078, end_log_pos 950241265. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
  76.   Replicate_Ignore_Server_Ids:
  77.              Master_Server_Id: 1000403307
  78.                   Master_UUID: 1d0963e9-85d9-11ef-80e1-00163e28e06a
  79.              Master_Info_File: mysql.slave_master_info
  80.                     SQL_Delay: 0
  81.           SQL_Remaining_Delay: NULL
  82.       Slave_SQL_Running_State:
  83.            Master_Retry_Count: 86400
  84.                   Master_Bind:
  85.       Last_IO_Error_Timestamp:
  86.      Last_SQL_Error_Timestamp: 241105 19:06:59
  87.                Master_SSL_Crl:
  88.            Master_SSL_Crlpath:
  89.            Retrieved_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-163235
  90.             Executed_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-163234
  91.                 Auto_Position: 1
  92.          Replicate_Rewrite_DB:
  93.                  Channel_Name:
  94.            Master_TLS_Version:
  95.        Master_public_key_path:
  96.         Get_master_public_key: 0
  97.             Network_Namespace:
  98. 1 row in set, 1 warning (0.00 sec)
  99. greatsql> SET sql_log_bin=0;
  100. Query OK, 0 rows affected (0.00 sec)
  101. greatsql> ALTER TABLE qj.sbtest1 ADD index tmp_k(k);
  102. Query OK, 0 rows affected (4 min 51.92 sec)
  103. Records: 0  Duplicates: 0  Warnings: 0
  104. greatsql> START SLAVE;
  105. Query OK, 0 rows affected, 1 warning (0.02 sec)
  106. greatsql> SHOW SLAVE STATUS \G
  107. *************************** 1. row ***************************
  108.                Slave_IO_State: Waiting for source to send event
  109.                   Master_Host: 172.17.139.230
  110.                   Master_User: greatsql
  111.                   Master_Port: 3307
  112.                 Connect_Retry: 60
  113.               Master_Log_File: binlog.000078
  114.           Read_Master_Log_Pos: 953750186
  115.                Relay_Log_File: relaylog.000231
  116.                 Relay_Log_Pos: 953750396
  117.         Relay_Master_Log_File: binlog.000078
  118.              Slave_IO_Running: Yes
  119.             Slave_SQL_Running: Yes
  120.               Replicate_Do_DB:
  121.           Replicate_Ignore_DB:
  122.            Replicate_Do_Table:
  123.        Replicate_Ignore_Table:
  124.       Replicate_Wild_Do_Table:
  125.   Replicate_Wild_Ignore_Table:
  126.                    Last_Errno: 0
  127.                    Last_Error:
  128.                  Skip_Counter: 0
  129.           Exec_Master_Log_Pos: 953750186
  130.               Relay_Log_Space: 953750683
  131.               Until_Condition: None
  132.                Until_Log_File:
  133.                 Until_Log_Pos: 0
  134.            Master_SSL_Allowed: No
  135.            Master_SSL_CA_File:
  136.            Master_SSL_CA_Path:
  137.               Master_SSL_Cert:
  138.             Master_SSL_Cipher:
  139.                Master_SSL_Key:
  140.         Seconds_Behind_Master: 0
  141. Master_SSL_Verify_Server_Cert: No
  142.                 Last_IO_Errno: 0
  143.                 Last_IO_Error:
  144.                Last_SQL_Errno: 0
  145.                Last_SQL_Error:
  146.   Replicate_Ignore_Server_Ids:
  147.              Master_Server_Id: 1000403307
  148.                   Master_UUID: 1d0963e9-85d9-11ef-80e1-00163e28e06a
  149.              Master_Info_File: mysql.slave_master_info
  150.                     SQL_Delay: 0
  151.           SQL_Remaining_Delay: NULL
  152.       Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
  153.            Master_Retry_Count: 86400
  154.                   Master_Bind:
  155.       Last_IO_Error_Timestamp:
  156.      Last_SQL_Error_Timestamp:
  157.                Master_SSL_Crl:
  158.            Master_SSL_Crlpath:
  159.            Retrieved_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-163235
  160.             Executed_Gtid_Set: 1d0963e9-85d9-11ef-80e1-00163e28e06a:1-163235
  161.                 Auto_Position: 1
  162.          Replicate_Rewrite_DB:
  163.                  Channel_Name:
  164.            Master_TLS_Version:
  165.        Master_public_key_path:
  166.         Get_master_public_key: 0
  167.             Network_Namespace:
  168. 1 row in set, 1 warning (0.00 sec)
复制代码
2、正在回放数据时,STOP SLAVE是否会有问题
  1. #为保持主从表结构一致,主节点添加索引
  2. greatsql> SET sql_log_bin=0;
  3. greatsql> ALTER TABLE qj.sbtest1 ADD index tmp_k(k);
复制代码
3、如果主从布局作为数据节点,上层还有计算节点该如何保持元数据的划一性即计算节点记载的表的布局和数据节点的表布局要划一
  1. greatsql> STOP SLAVE --当该GTID回放很长时间时,stop可能会很长时间,我们可以采用 kill 线程方式结束回放
  2. greatsql> SHOW PROCESSLIST; --找到会话ID
  3. greatsql> KILL 会话号
  4. greatsql> SHOW SLAVE STATUS  \G
  5. -- 添加索引即可
复制代码
五、总结

加快无索引表的回放重要是针对该表上进行 DELETE 或 UPDATE 操作时有用,而且我们操作的时候要注意是否能记载binlog日志,保证主从数据划一性,当从节点作为数据节点即存储节点的角色时,要关注计算节点即代理层元数据与存储节点元数据的划一性或者是符合计算节点关于该表的定义规则。

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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

南飓风

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表