爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      【DB宝34】测试MySQL主从复制中主库表缺失主键会导致主从延迟的情况

      首页 知识中心 软件开发 文章详情页

      【DB宝34】测试MySQL主从复制中主库表缺失主键会导致主从延迟的情况

      2023-06-15 06:23:56 阅读次数:433

      MySQL,主从复制

      目录

      一、简介二、环境准备三、实验过程  3.1 主库创建表  3.2 主库做更新操作  3.3 分析主库的binlog日志  3.4 分析从库的中继日志四、结论
        一、简介

      导致MySQL主从复制延迟的原因有很多,其中一个原因就是大表缺失主键或唯一索引。

      今天我们就通过实验的方式来验证这种情况。

       二、环境准备

      主库:IP为192.168.68.168,端口3306,版本为8.0.20

      从库:IP为192.168.68.168,端口3306,版本为8.0.20

       1[root@docker35 ~]# docker ps
       2CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
       3ebe3b62a2358        mysql:8.0.20        "docker-entrypoint.s…"   8 days ago          Up 8 days           33060/tcp, 0.0.0.0:3319->3306/tcp   mysql8020S1
       476140b04e2fd        mysql:8.0.20        "docker-entrypoint.s…"   8 days ago          Up 8 days           33060/tcp, 0.0.0.0:3318->3306/tcp   mysql8020M1
       5
       6
       7-- 主库
       8MySQL [lhrdb1]> show slave hosts;
       9+-----------+------+------+-----------+--------------------------------------+
      10| Server_id | Host | Port | Master_id | Slave_UUID                           |
      11+-----------+------+------+-----------+--------------------------------------+
      12|  80203319 |      | 3306 |  80203318 | e12dfcd2-1e40-11eb-b2f0-0242c0a844a9 |
      13+-----------+------+------+-----------+--------------------------------------+
      14
      15MySQL [lhrdb1]> show variables like '%binlog_format%';
      16+---------------+-------+
      17| Variable_name | Value |
      18+---------------+-------+
      19| binlog_format | ROW   |
      20+---------------+-------+
      211 row in set (0.00 sec)
      22
      23
      24-- 从库
      25MySQL [(none)]> show slave status\G;
      26*************************** 1. row ***************************
      27               Slave_IO_State: Waiting for master to send event
      28                  Master_Host: 192.168.68.168
      29                  Master_User: repl
      30                  Master_Port: 3306
      31                Connect_Retry: 60
      32              Master_Log_File: mysql8020M1-bin.000007
      33          Read_Master_Log_Pos: 19665393
      34               Relay_Log_File: mysql8020S1-relay-bin.000008
      35                Relay_Log_Pos: 19665620
      36        Relay_Master_Log_File: mysql8020M1-bin.000007
      37             Slave_IO_Running: Yes
      38            Slave_SQL_Running: Yes
      39              Replicate_Do_DB:
      40          Replicate_Ignore_DB: information_schema,performance_schema,mysql,sys
      41           Replicate_Do_Table:
      42       Replicate_Ignore_Table:
      43      Replicate_Wild_Do_Table:
      44  Replicate_Wild_Ignore_Table:
      45                   Last_Errno: 0
      46                   Last_Error:
      47                 Skip_Counter: 0
      48          Exec_Master_Log_Pos: 19665393
      49              Relay_Log_Space: 19665928
      50              Until_Condition: None
      51               Until_Log_File:
      52                Until_Log_Pos: 0
      53           Master_SSL_Allowed: No
      54           Master_SSL_CA_File:
      55           Master_SSL_CA_Path:
      56              Master_SSL_Cert:
      57            Master_SSL_Cipher:
      58               Master_SSL_Key:
      59        Seconds_Behind_Master: 0
      60Master_SSL_Verify_Server_Cert: No
      61                Last_IO_Errno: 0
      62                Last_IO_Error:
      63               Last_SQL_Errno: 0
      64               Last_SQL_Error:
      65  Replicate_Ignore_Server_Ids:
      66             Master_Server_Id: 80203318
      67                  Master_UUID: dcccf122-1e40-11eb-8ca0-0242c0a844a8
      68             Master_Info_File: mysql.slave_master_info
      69                    SQL_Delay: 0
      70          SQL_Remaining_Delay: NULL
      71      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
      72           Master_Retry_Count: 86400
      73                  Master_Bind:
      74      Last_IO_Error_Timestamp:
      75     Last_SQL_Error_Timestamp:
      76               Master_SSL_Crl:
      77           Master_SSL_Crlpath:
      78           Retrieved_Gtid_Set: dcccf122-1e40-11eb-8ca0-0242c0a844a8:20-160037
      79            Executed_Gtid_Set: dcccf122-1e40-11eb-8ca0-0242c0a844a8:1-160037
      80                Auto_Position: 1
      81         Replicate_Rewrite_DB:
      82                 Channel_Name:
      83           Master_TLS_Version:
      84       Master_public_key_path:
      85        Get_master_public_key: 0
      86            Network_Namespace:
      871 row in set (0.00 sec)
           三、实验过程 

      3.1 主库创建表

      主库先创建一张8万行的大表:

       1MySQL [lhrdb1]> DELIMITER $$
       2MySQL [lhrdb1]> drop procedure if exists `t_pro`$$
       3Query OK, 0 rows affected (0.01 sec)
       4
       5MySQL [lhrdb1]> create procedure `t_pro`(num int)
       6    -> begin
       7    -> declare i int unsigned default 1;
       8    -> set i=1;
       9    ->  while i <= num do
      10    ->   insert into `t` (`id`,`name`)
      11    ->    values(i,concat('主键测试',i));
      12    ->   set i=i + 1;
      13    ->  end while;
      14    -> end$$
      15Query OK, 0 rows affected (0.01 sec)
      16
      17MySQL [lhrdb1]> DELIMITER ;
      18MySQL [lhrdb1]> call `t_pro`(80000);
      19
      20--- .... 这里插入8万行数据,比较慢
       

      从库查询延迟:

       1D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
       2mysql: [Warning] Using a password on the command line interface can be insecure.
       3        Seconds_Behind_Master: 11
       4
       5D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G"
       6mysql: [Warning] Using a password on the command line interface can be insecure.
       7*************************** 1. row ***************************
       8               Slave_IO_State: Waiting for master to send event
       9                  Master_Host: 192.168.68.168
      10                  Master_User: repl
      11                  Master_Port: 3306
      12                Connect_Retry: 60
      13              Master_Log_File: mysql8020M1-bin.000007
      14          Read_Master_Log_Pos: 26029948
      15               Relay_Log_File: mysql8020S1-relay-bin.000008
      16                Relay_Log_Pos: 25067097
      17        Relay_Master_Log_File: mysql8020M1-bin.000007
      18             Slave_IO_Running: Yes
      19            Slave_SQL_Running: Yes
      20              Replicate_Do_DB:
      21          Replicate_Ignore_DB: information_schema,performance_schema,mysql,sys
      22           Replicate_Do_Table:
      23       Replicate_Ignore_Table:
      24      Replicate_Wild_Do_Table:
      25  Replicate_Wild_Ignore_Table:
      26                   Last_Errno: 0
      27                   Last_Error:
      28                 Skip_Counter: 0
      29          Exec_Master_Log_Pos: 25066870
      30              Relay_Log_Space: 26030483
      31              Until_Condition: None
      32               Until_Log_File:
      33                Until_Log_Pos: 0
      34           Master_SSL_Allowed: No
      35           Master_SSL_CA_File:
      36           Master_SSL_CA_Path:
      37              Master_SSL_Cert:
      38            Master_SSL_Cipher:
      39               Master_SSL_Key:
      40        Seconds_Behind_Master: 12
      41Master_SSL_Verify_Server_Cert: No
      42                Last_IO_Errno: 0
      43                Last_IO_Error:
      44               Last_SQL_Errno: 0
      45               Last_SQL_Error:
      46  Replicate_Ignore_Server_Ids:
      47             Master_Server_Id: 80203318
      48                  Master_UUID: dcccf122-1e40-11eb-8ca0-0242c0a844a8
      49             Master_Info_File: mysql.slave_master_info
      50                    SQL_Delay: 0
      51          SQL_Remaining_Delay: NULL
      52      Slave_SQL_Running_State: waiting for handler commit
      53           Master_Retry_Count: 86400
      54                  Master_Bind:
      55      Last_IO_Error_Timestamp:
      56     Last_SQL_Error_Timestamp:
      57               Master_SSL_Crl:
      58           Master_SSL_Crlpath:
      59           Retrieved_Gtid_Set: dcccf122-1e40-11eb-8ca0-0242c0a844a8:20-181149
      60            Executed_Gtid_Set: dcccf122-1e40-11eb-8ca0-0242c0a844a8:1-177960
      61                Auto_Position: 1
      62         Replicate_Rewrite_DB:
      63                 Channel_Name:
      64           Master_TLS_Version:
      65       Master_public_key_path:
      66        Get_master_public_key: 0
      67            Network_Namespace:
      68
      69D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      70mysql: [Warning] Using a password on the command line interface can be insecure.
      71        Seconds_Behind_Master: 19 
      72
      73MySQL [(none)]> select count(*) from lhrdb1.t;
      74+----------+
      75| count(*) |
      76+----------+
      77|    41613 |
      78+----------+
      791 row in set (0.01 sec)
      80
      81MySQL [(none)]> select count(*) from lhrdb1.t;
      82+----------+
      83| count(*) |
      84+----------+
      85|    41941 |
      86+----------+
      871 row in set (0.01 sec)        
      88
       

      发现,从库延迟越来越高,主要原因是主库正在进行大批量的数据插入操作。

      大约5分钟后,主库执行完毕,

       1MySQL [lhrdb1]> call `t_pro`(80000);
       2Query OK, 1 row affected (5 min 3.37 sec)
       3
       4MySQL [lhrdb1]> select count(*) from lhrdb1.t;
       5+----------+
       6| count(*) |
       7+----------+
       8|    80000 |
       9+----------+
      101 row in set (0.01 sec)
       

      从库查询,数据已同步完成:

       1D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
       2mysql: [Warning] Using a password on the command line interface can be insecure.
       3        Seconds_Behind_Master: 30
       4
       5D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
       6mysql: [Warning] Using a password on the command line interface can be insecure.
       7        Seconds_Behind_Master: 0
       8
       9MySQL [lhrdb1]> select count(*) from lhrdb1.t;
      10+----------+
      11| count(*) |
      12+----------+
      13|    80000 |
      14+----------+
      151 row in set (0.01 sec)        
      16
           

      3.2 主库做更新操作

       1MySQL [lhrdb1]> flush logs;
       2Query OK, 0 rows affected (0.02 sec)
       3
       4MySQL [lhrdb1]>
       5MySQL [lhrdb1]> show master logs;
       6+------------------------+-----------+-----------+
       7| Log_name               | File_size | Encrypted |
       8+------------------------+-----------+-----------+
       9| mysql8020M1-bin.000001 |       179 | No        |
      10| mysql8020M1-bin.000002 |       179 | No        |
      11| mysql8020M1-bin.000003 |       179 | No        |
      12| mysql8020M1-bin.000004 |  53350509 | No        |
      13| mysql8020M1-bin.000005 |   1155468 | No        |
      14| mysql8020M1-bin.000006 |   1345290 | No        |
      15| mysql8020M1-bin.000007 |  45018719 | No        |
      16| mysql8020M1-bin.000008 |       196 | No        |
      17+------------------------+-----------+-----------+
      188 rows in set (0.00 sec)
      19
      20MySQL [lhrdb1]> update t set name=concat('主键测试,结果验证',t.id) where id <=60000;
      21Query OK, 40000 rows affected (1.88 sec)
      22Rows matched: 60000  Changed: 40000  Warnings: 0
       

      可以看出,主库基本在2s就更新完成,变化的行数为4万行。

      从库查询延迟,

       1D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
       2mysql: [Warning] Using a password on the command line interface can be insecure.
       3        Seconds_Behind_Master: 0
       4
       5D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
       6mysql: [Warning] Using a password on the command line interface can be insecure.
       7        Seconds_Behind_Master: 0
       8
       9D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      10mysql: [Warning] Using a password on the command line interface can be insecure.
      11        Seconds_Behind_Master: 4
      12
      13D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      14mysql: [Warning] Using a password on the command line interface can be insecure.
      15        Seconds_Behind_Master: 5
      16
      17D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      18mysql: [Warning] Using a password on the command line interface can be insecure.
      19        Seconds_Behind_Master: 7
      20
      21D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      22mysql: [Warning] Using a password on the command line interface can be insecure.
      23        Seconds_Behind_Master: 8
      24
      25D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      26mysql: [Warning] Using a password on the command line interface can be insecure.
      27        Seconds_Behind_Master: 10
      28
      29D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      30mysql: [Warning] Using a password on the command line interface can be insecure.
      31        Seconds_Behind_Master: 11
      32
      33D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      34mysql: [Warning] Using a password on the command line interface can be insecure.
      35        Seconds_Behind_Master: 13
      36
      37D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      38mysql: [Warning] Using a password on the command line interface can be insecure.
      39        Seconds_Behind_Master: 14
      40
      41D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      42mysql: [Warning] Using a password on the command line interface can be insecure.
      43        Seconds_Behind_Master: 16
      44
      45D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      46mysql: [Warning] Using a password on the command line interface can be insecure.
      47        Seconds_Behind_Master: 17
      48
      49D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      50mysql: [Warning] Using a password on the command line interface can be insecure.
      51        Seconds_Behind_Master: 18
      52
      53D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      54mysql: [Warning] Using a password on the command line interface can be insecure.
      55        Seconds_Behind_Master: 19
      56
      57D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      58mysql: [Warning] Using a password on the command line interface can be insecure.
      59        Seconds_Behind_Master: 20
      60
      61D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      62mysql: [Warning] Using a password on the command line interface can be insecure.
      63        Seconds_Behind_Master: 21
      64
      65D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      66mysql: [Warning] Using a password on the command line interface can be insecure.
      67        Seconds_Behind_Master: 0
      68
      69D:\Program Files\MySQL\mysql-8.0.15-winx64\bin>mysql -uroot -plhr -h192.168.1.35 -P3319 -e "show slave status \G" | grep Seconds_Behind_Master
      70mysql: [Warning] Using a password on the command line interface can be insecure.
      71        Seconds_Behind_Master: 0
      72
       

      可以发现,最长延迟21秒左右。

       

      3.3 分析主库的binlog日志

       1root@mysql8020M1:/var/lib/mysql# mysqlbinlog mysql8020M1-bin.000008 --base64-output=decode-row -vv | grep UPDATE  | wc -l 
       240000
       3root@mysql8020M1:/var/lib/mysql# mysqlbinlog mysql8020M1-bin.000008 --base64-output=decode-row -vv | more                
       4/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
       5/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
       6DELIMITER /*!*/;
       7# at 4
       8#201112 11:25:24 server id 80203318  end_log_pos 125 CRC32 0x11d2d479   Start: binlog v 4, server v 8.0.20 created 201112 11:25:24
       9# Warning: this binlog is either in use or was not closed properly.
      10# at 125
      11#201112 11:25:24 server id 80203318  end_log_pos 196 CRC32 0x443aeae4   Previous-GTIDs
      12# dcccf122-1e40-11eb-8ca0-0242c0a844a8:1-240042
      13# at 196
      14#201112 11:25:33 server id 80203318  end_log_pos 276 CRC32 0xa8eb8112   GTID    last_committed=0        sequence_number=1       rbr_only=yes    original_committed_timestamp=1605151535412817   immediate_commit_timestamp=1605151535412817     transaction_length=2451018
      15/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
      16# original_commit_timestamp=1605151535412817 (2020-11-12 11:25:35.412817 CST)
      17# immediate_commit_timestamp=1605151535412817 (2020-11-12 11:25:35.412817 CST)
      18/*!80001 SET @@session.original_commit_timestamp=1605151535412817*//*!*/;
      19/*!80014 SET @@session.original_server_version=80020*//*!*/;
      20/*!80014 SET @@session.immediate_server_version=80020*//*!*/;
      21SET @@SESSION.GTID_NEXT= 'dcccf122-1e40-11eb-8ca0-0242c0a844a8:240043'/*!*/;
      22# at 276
      23#201112 11:25:33 server id 80203318  end_log_pos 362 CRC32 0x0b667ff4   Query   thread_id=26    exec_time=0     error_code=0
      24SET TIMESTAMP=1605151533/*!*/;
      25SET @@session.pseudo_thread_id=26/*!*/;
      26SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
      27SET @@session.sql_mode=1168113696/*!*/;
      28SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
      29
      30-- 省略部分  ..................
      31#201112 11:25:33 server id 80203318  end_log_pos 2446999 CRC32 0x6e0e09e8       Update_rows: table id 105
      32# at 2446999
      33#201112 11:25:33 server id 80203318  end_log_pos 2451183 CRC32 0x30bfc23e       Update_rows: table id 105 flags: STMT_END_F
      34### UPDATE `lhrdb1`.`t`
      35### WHERE
      36###   @1=20001 /* INT meta=0 nullable=0 is_null=0 */
      37###   @2='主键测试20001' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      38### SET
      39###   @1=20001 /* INT meta=0 nullable=0 is_null=0 */
      40###   @2='主键测试,结果验证20001' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      41### UPDATE `lhrdb1`.`t`
      42### WHERE
      43###   @1=20002 /* INT meta=0 nullable=0 is_null=0 */
      44###   @2='主键测试20002' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      45### SET
      46###   @1=20002 /* INT meta=0 nullable=0 is_null=0 */
      47###   @2='主键测试,结果验证20002' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      48
      49-- 省略部分  ..................
      50
      51root@mysql8020M1:/var/lib/mysql# mysqlbinlog mysql8020M1-bin.000008 --base64-output=decode-row -vv | tail -n 20
      52###   @1=59999 /* INT meta=0 nullable=0 is_null=0 */
      53###   @2='主键测试59999' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      54### SET
      55###   @1=59999 /* INT meta=0 nullable=0 is_null=0 */
      56###   @2='主键测试,结果验证59999' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      57### UPDATE `lhrdb1`.`t`
      58### WHERE
      59###   @1=60000 /* INT meta=0 nullable=0 is_null=0 */
      60###   @2='主键测试60000' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      61### SET
      62###   @1=60000 /* INT meta=0 nullable=0 is_null=0 */
      63###   @2='主键测试,结果验证60000' /* VARSTRING(80) meta=80 nullable=0 is_null=0 */
      64# at 2451183
      65#201112 11:25:33 server id 80203318  end_log_pos 2451214 CRC32 0x3d5db696       Xid = 720239
      66COMMIT/*!*/;
      67SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
      68DELIMITER ;
      69# End of log file
      70/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
      71/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
       

      可以看出,在ROW模式下,在主库上执行了一条UPDATE语句,更新了4万行记录,但是在binlog中,记录了4万行的UPDATE语句。

      3.4 分析从库的中继日志

      1root@mysql8020S1:/var/lib/mysql# mysqlbinlog mysql8020S1-relay-bin.000010 --base64-output=decode-row -vv | grep UPDATE | wc -l
      240000

      可以看出,在从库上也是4万行的UPDATE语句,也是一条一条的进行更新。由于没有主键和索引,所以,就会导致在从库进行4万次的全表扫描,这样也就拖慢了从库APPLY的效率。

       四、结论

      在MySQL的主从复制架构中,若存在大表,那么一定要有主键或唯一索引,否则将导致很大的主从延迟。

      本文结束。

      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/lhrbest/2695358,作者:小麦苗DB宝,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:SpringBoot-RabbitMQ消息的消费与签收机制

      下一篇:Linux脚本练习之script017-接受一个主机的 IPv4 地址作为参数,测试是否可连通。如果能够 ping 通,则提示用户“该 IP 地址可访问”。

      相关文章

      2025-05-19 09:05:01

      Navicat 连接MySQL 8.0.11 出现2059错误 解决

      Navicat 连接MySQL 8.0.11 出现2059错误 解决

      2025-05-19 09:05:01
      MySQL , Navicat , 解决 , 连接
      2025-05-19 09:04:38

      mysql只有在任务处于完成状态才能运行

      mysql只有在任务处于完成状态才能运行

      2025-05-19 09:04:38
      MySQL , 任务 , 数据库 , 查询 , 状态
      2025-05-16 09:15:24

      MySQL 表的内外连接

      MySQL 表的内外连接

      2025-05-16 09:15:24
      MySQL , 显示 , 连接
      2025-05-14 10:03:13

      MySQL 索引优化以及慢查询优化

      MySQL 是一种广泛使用的关系型数据库管理系统,因其性能优异和使用便捷而备受欢迎。然而,随着数据量的增长和查询复杂度的增加,性能瓶颈也变得越来越明显。

      2025-05-14 10:03:13
      MySQL , 优化 , 使用 , 性能 , 数据库 , 查询 , 索引
      2025-05-14 09:51:21

      mysql数据库中decimal数据类型比较大小

      在MySQL中,DECIMAL数据类型用于存储精确的数值,它非常适合用于需要高精度计算的场景,如金融应用。当我们需要在MySQL数据库中比较DECIMAL类型数据的大小时,可以使用标准的比较运算符,如>, <, >=, <=, = 和 <>(或!=)。

      2025-05-14 09:51:21
      MySQL , SQL , 数据类型 , 查询 , 比较 , 示例
      2025-05-14 09:51:15

      mysql 语句如何优化

      MySQL语句的优化是一个复杂但重要的过程,因为它可以显著提高数据库的性能。

      2025-05-14 09:51:15
      JOIN , MySQL , 优化 , 使用 , 排序 , 查询 , 索引
      2025-05-13 09:53:13

      mysql 存储函数及调用

      在MySQL中,存储函数(Stored Function)是一种在数据库中定义的特殊类型的函数,它可以从一个或多个参数返回一个值。存储函数在数据库层面上封装了复杂的SQL逻辑,使得在应用程序中调用时更加简单和高效。

      2025-05-13 09:53:13
      MySQL , 函数 , 存储 , 示例 , 触发器 , 语句 , 调用
      2025-05-13 09:49:12

      JDBC事务管理、四大特征(ACID)、事务提交与回滚、MySQL事务管理

      JDBC(Java Database Connectivity)事务是指一系列作为单个逻辑工作单元执行的数据库操作,这些操作要么全部成功——>提交,要么全部失败——>回滚,从而确保数据的一致性和完整性。

      2025-05-13 09:49:12
      MySQL , 事务 , 执行 , 提交 , 操作 , 数据库
      2025-05-08 09:04:49

      MySQL-备份+日志:介质故障与数据库恢复

      MySQL-备份+日志:介质故障与数据库恢复

      2025-05-08 09:04:49
      mysql , MySQL , 备份 , 恢复 , 数据库 , 文件 , 日志
      2025-05-06 09:19:51

      MySQL 数据库备份与恢复详解

      数据库备份是防止数据丢失的有效手段。无论是由于硬件故障、软件问题还是人为错误,定期备份都能有效保护数据。适当的备份策略可以在数据损失时快速恢复系统,减少业务损失。

      2025-05-06 09:19:51
      bash , MySQL , 备份 , 恢复 , 数据库
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5225204

      查看更多

      最新文章

      DataGrip测试连接时出现报错解决方案

      2025-01-06 08:37:58

      mysql 存储过程返回更新前记录

      2024-12-27 07:57:35

      MySQL 取相同数据里面其中一个值最大的数据

      2024-12-20 07:51:29

      使用Spring Boot集成MySQL数据库

      2024-11-19 09:56:50

      MySQL字段默认值设置详解

      2024-10-11 08:56:56

      《深入浅出MySQL--数据库开发、优化与管理维护》读书笔记--开发篇

      2024-07-08 08:10:47

      查看更多

      热门文章

      JAVA之MySQL数据库增删改查【Java入门练手】

      2023-04-23 09:38:11

      软件开发入门教程网之MySQL 索引

      2023-05-22 06:33:14

      掌控MySQL并发:深度解析锁机制与并发控制

      2023-06-14 09:10:42

      找出未提交的MySQL线程/事务

      2023-06-28 09:04:09

      Java读写MySQL数据库小实例

      2023-04-13 10:26:25

      软件开发入门教程网 Search之​​DELETE 语句

      2023-05-17 06:47:57

      查看更多

      热门标签

      java Java python 编程开发 代码 开发语言 算法 线程 Python html 数组 C++ 元素 javascript c++
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      JAVA之MySQL数据库增删改查【Java入门练手】

      DataGrip测试连接时出现报错解决方案

      找出未提交的MySQL线程/事务

      MySQL输出html格式文件

      【Java代码实现检查库存、生成订单并存入MySQL数据库】

      【EXCEL数据导入mysql数据库的程序】

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号