复制延迟(Seconds_Behind_Master)测试

测试环境: MySQL 5.7.19

测试主从时间差:

检查主从系统时间差,同时在主库和从库执行SELECT NOW()语句:
主库:-- ::
从库:-- ::
从库比主库提前14秒,主从时间差14秒。

在主库上执行(使用基于语句格式复制):

select now();
update tb003 set c1=SLEEP() where id=;
select now();

主库上上执行效果:

mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.00 sec) mysql> update tb003 set c1=SLEEP() where id=;
Query OK, row affected, warning (20.06 sec)
Rows matched: Changed: Warnings: mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.00 sec)

主库上生成的binlog解析结果:

# :: server id   end_log_pos  CRC32 0xcf2313f1     GTID    last_committed=    sequence_number=    rbr_only=no
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:9'/*!*/;
# at
# :: server id end_log_pos CRC32 0xa14a2bcb Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0xc283c9af Query thread_id= exec_time= error_code=
use `db001`/*!*/;
SET TIMESTAMP=/*!*/;
update tb003 set c1=SLEEP() where id=
/*!*/;
# at
# :: server id end_log_pos CRC32 0x0f3bdc57 Xid =
COMMIT/*!*/;

从库上relay log解析结果:

BINLOG '
+zwQXQ+4ohgAdwAAAAAAAAAgAAQANS43LjE5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
Af0UlBk=
'/*!*/;
# at
# :: server id end_log_pos CRC32 0xcf2313f1 GTID last_committed= sequence_number= rbr_only=no
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:9'/*!*/;
# at
# :: server id end_log_pos CRC32 0xa14a2bcb Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0xc283c9af Query thread_id= exec_time= error_code=
use `db001`/*!*/;
SET TIMESTAMP=/*!*/;
update tb003 set c1=SLEEP() where id=
/*!*/;
# at
# :: server id end_log_pos CRC32 0x0f3bdc57 Xid =
COMMIT/*!*/;

从库上生成binlog解析结果(从库上默认binlog_format=row):

# :: server id   end_log_pos  CRC32 0x6e1a971f     GTID    last_committed=    sequence_number=    rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:9'/*!*/;
# at
# :: server id end_log_pos CRC32 0x063cd6ca Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0x405b61c7 Rows_query
# update tb003 set c1=SLEEP() where id=
# at
# :: server id end_log_pos CRC32 0x015803f1 Table_map: `db001`.`tb003` mapped to number
# at
# :: server id end_log_pos CRC32 0xb59069f9 Update_rows: table id flags: STMT_END_F BINLOG '
Ij0QXR24ohgAQAAAAIwBAACAACh1cGRhdGUgdGIwMDMgc2V0IGMxPVNMRUVQKDIwKSB3aGVyZSBp
ZD0xx2FbQA==
Ij0QXRO4ohgANAAAAMABAAAAAOgAAAAAAAEABWRiMDAxAAV0YjAwMwADCAMRAQAC8QNYAQ==
Ij0QXR+4ohgARgAAAAYCAAAAAOgAAAAAAAEAAgAD///4AQAAAAAAAAABAAAAXRA89fgBAAAAAAAA
AAAAAABdED0i+WmQtQ==
'/*!*/;
### UPDATE `db001`.`tb003`
### WHERE
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
### SET
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
# at
# :: server id end_log_pos CRC32 0xae037515 Xid =
COMMIT/*!*/;

无论时主库binlog事件中还是从库relaylog事件以及从库binlog事件中记录的时间都是事务语句执行开始时间:

SET TIMESTAMP=/*!*/;

SELECT FROM_UNIXTIME();
+---------------------------+
| FROM_UNIXTIME() |
+---------------------------+
| -- :: |
+---------------------------+

exec_time计算:The time from when the query started to when it was logged in the binlog, in seconds.

1、由于SQL语句中使用函数SLEEP(20),因此SQL语句执行时间=SLEEP(20)的执行时间+UPDATE操作的执行时间,而UPDATE操作的执行时间在10ms以内,因此整个SQL语句在主库的执行时间=20S,因此主库binlog的中记录exec_time=20

2、从库的relaylog用来存放主库传递过来的binlog,因此从库relaylog与主库binlog相同,从库relaylog中记录也是exec_time=20

3、由于主库使用binlog_format=statement的复制格式,因此SQL语句被传递到从库并原样执行,同样执行需要20S,但该语句在主库的开始执行时间(the query started)为2019-06-24 11:01:54,而在从库执行结束时间(log in binlog)为2019-06-24 11:02:48,因此从库binlog中记录时间exec_time=54,忽略主从事务日志落盘和网络传输时间,exec_time=54S=主库语句执行时间(20)+从库语句执行时间(20S)+主从时间差(14S)。

在主库上执行(使用基于行格式复制):

mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.00 sec) mysql> update tb003 set c1=SLEEP() where id in (,);
Query OK, rows affected (40.04 sec)
Rows matched: Changed: Warnings: mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.00 sec)

产生的binlog解析结果为:

# at
# :: server id end_log_pos CRC32 0x037e9b24 GTID last_committed= sequence_number= rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:65'/*!*/;
# at
# :: server id end_log_pos CRC32 0xe186daee Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0x9f76a54a Rows_query
# update tb003 set c1=SLEEP() where id in (,)
# at
# :: server id end_log_pos CRC32 0x139892d9 Table_map: `db001`.`tb003` mapped to number
# at
# :: server id end_log_pos CRC32 0xf7980c93 Update_rows: table id flags: STMT_END_F BINLOG '
ZW0RXR24ohgARwAAAPYCAACAAC91cGRhdGUgdGIwMDMgc2V0IGMxPVNMRUVQKDIwKSB3aGVyZSBp
ZCBpbiAoMyw0KUqldp8=
ZW0RXRO4ohgANAAAACoDAAAAAGwAAAAAAAEABWRiMDAxAAV0YjAwMwADCAMRAQAC2ZKYEw==
ZW0RXR+4ohgAaAAAAJIDAAAAAGwAAAAAAAEAAgAD///4AwAAAAAAAAABAAAAXRFrW/gDAAAAAAAA
AAAAAABdEW1l+AQAAAAAAAAAAQAAAF0Ra1v4BAAAAAAAAAAAAAAAXRFtZZMMmPc=
'/*!*/;
### UPDATE `db001`.`tb003`
### WHERE
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
### SET
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
### UPDATE `db001`.`tb003`
### WHERE
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
### SET
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
# at
# :: server id end_log_pos CRC32 0x1f65c801 Xid =
COMMIT/*!*/;

在主库上执行(使用基于行格式复制+两条语句组合事务操作):

mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.00 sec) mysql> begin;
Query OK, rows affected (0.00 sec) mysql> update tb003 set c1=SLEEP() where id =;
Query OK, row affected (20.00 sec)
Rows matched: Changed: Warnings: mysql> update tb003 set c1=SLEEP() where id =;
Query OK, row affected (20.00 sec)
Rows matched: Changed: Warnings: mysql> commit;
Query OK, rows affected (0.05 sec) mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.00 sec)

产生的binlog解析结果为:

# at
# :: server id end_log_pos CRC32 0x5a8cb5f0 GTID last_committed= sequence_number= rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '025fd638-89ea-11e9-a749-40f2e9cf3aaa:66'/*!*/;
# at
# :: server id end_log_pos CRC32 0x5217a928 Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0x1a70cac7 Rows_query
# update tb003 set c1=SLEEP() where id =
# at
# :: server id end_log_pos CRC32 0x4cf6d8c7 Table_map: `db001`.`tb003` mapped to number
# at
# :: server id end_log_pos CRC32 0x033f26f8 Update_rows: table id flags: STMT_END_F BINLOG '
HHERXR24ohgAQQAAAHwEAACAACl1cGRhdGUgdGIwMDMgc2V0IGMxPVNMRUVQKDIwKSB3aGVyZSBp
ZCA9NcfKcBo=
HHERXRO4ohgANAAAALAEAAAAAGwAAAAAAAEABWRiMDAxAAV0YjAwMwADCAMRAQACx9j2TA==
HHERXR+4ohgARgAAAPYEAAAAAGwAAAAAAAEAAgAD///4BQAAAAAAAAABAAAAXRDBV/gFAAAAAAAA
AAAAAABdEXEc+CY/Aw==
'/*!*/;
### UPDATE `db001`.`tb003`
### WHERE
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
### SET
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
# at
# :: server id end_log_pos CRC32 0x3b3202fa Rows_query
# update tb003 set c1=SLEEP() where id =
# at
# :: server id end_log_pos CRC32 0x8b013c05 Table_map: `db001`.`tb003` mapped to number
# at
# :: server id end_log_pos CRC32 0xf7d67996 Update_rows: table id flags: STMT_END_F BINLOG '
MHERXR24ohgAQQAAADcFAACAACl1cGRhdGUgdGIwMDMgc2V0IGMxPVNMRUVQKDIwKSB3aGVyZSBp
ZCA9NvoCMjs=
MHERXRO4ohgANAAAAGsFAAAAAGwAAAAAAAEABWRiMDAxAAV0YjAwMwADCAMRAQACBTwBiw==
MHERXR+4ohgARgAAALEFAAAAAGwAAAAAAAEAAgAD///4BgAAAAAAAAABAAAAXRB9NvgGAAAAAAAA
AAAAAABdEXEwlnnW9w==
'/*!*/;
### UPDATE `db001`.`tb003`
### WHERE
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
### SET
### @= /* LONGINT meta=0 nullable=0 is_null=0 */
### @= /* INT meta=0 nullable=1 is_null=0 */
### @= /* TIMESTAMP(0) meta=0 nullable=0 is_null=0 */
# at
# :: server id end_log_pos CRC32 0x8b074201 Xid =
COMMIT/*!*/;

可以发现上面两个操作都执行了40秒,但在事务开始前(BEGIN)的exec_time都仅为20,且事务内部单个操作没有exec_time。

ev->exec_time的计算在sql/log_event.cc中的代码如下:

  /*
exec_time calculation has changed to use the same method that is used
to fill out "thd_arg->start_time"
*/ struct timeval end_time;
ulonglong micro_end_time= my_micro_time();
my_micro_time_to_timeval(micro_end_time, &end_time); exec_time= end_time.tv_sec - thd_arg->start_time.tv_sec;

exec_time在文件sql/log_event.h中的注解如下:

  <tr>
<td>exec_time</td>
<td>4 byte unsigned integer</td>
<td>The time from when the query started to when it was logged in the binlog, in seconds.</td>
</tr>

MySQL Replication--复制延迟02--exec_time测试的更多相关文章

  1. 浅谈MySQL Replication(复制)基本原理

    1.MySQL Replication复制进程MySQL的复制(replication)是一个异步的复制,从一个MySQL instace(称之为Master)复制到另一个MySQL instance ...

  2. mysql replication 复制的一些问题

    1   过大的复制延迟 mysql 的复制延迟是一个常见问题,现在已经有一些解决方案,如淘宝开发的一些工具 2 没有磁盘空间 复制导致磁盘空间塞满,二进制日志.中继日志或临时文件把磁盘塞满,slave ...

  3. 在Docker平台实现MySQL Replication(复制)

    MySQL Replication提供了数据库之间复制数据的功能,通过这个功能可以让一个数据库的数据更改自动同步到另外一个数据库.通常用这个功能来实现数据备份.数据容灾.数据冗余,进一步实现数据的读写 ...

  4. mysql cp复制和mysqldump备份测试

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 备份策略 针对不同的场景下, 我们应该制定不同的备份策略对数据库进行 ...

  5. Windows 下MySql Replication(复制)配置

    环境准备 到官网下载mysql-installer-web-community-5.7.21.0.msi并安装,选择MySql Workbench,记录安装时root输入的密码. 需要安装在两台机器上 ...

  6. 你遇到过哪些原因造成MySQL异步复制延迟?

    master上多为并发事务,salve上则多为单线程回放(MySQL 5.7起,支持真正的并行回放,有所缓解) 异步复制,本来就是有一定延迟的(否则也不叫做异步了,介意的话可以改成半同步复制) sla ...

  7. MySQL异步复制延迟解决

    http://www.ttlsa.com/mysql/mysql-5-7-enhanced-multi-thread-salve/

  8. 浅析 MySQL Replication(本文转自网络,非本人所写)

    作者:卢飞 来源:DoDBA(mysqlcode) 0.导读 本文几乎涵盖了MySQL Replication(主从复制)的大部分知识点,包括Replication原理.binlog format.复 ...

  9. 浅析 MySQL Replication(转)

    目前很多公司中的生产环境中都使用了MySQL Replication ,也叫 MySQL 复制,搭建配置方便等很多特性让 MySQL Replication 的应用很广泛,我们曾经使用过一主拖20多个 ...

  10. 浅析 MySQL Replication(本文转自网络)

    作者:卢飞 来源:DoDBA(mysqlcode) 0.导读 本文几乎涵盖了MySQL Replication(主从复制)的大部分知识点,包括Replication原理.binlog format.复 ...

随机推荐

  1. 使用innobackupex进行mysql的差异备份还原和延迟复制

    使用innobackupex进行mysql的差异备份还原和延迟复制 背景: 有同事执行update语句没有添加where条件,导致大量脏数据,需要将这张表恢复到前一天 数据库上有备份,每周一次完整备份 ...

  2. linux安装redis时报collect2: fatal error: cannot find 'ld'和In file included from adlist.c:34:0:

    如题,看了下该ld命令所在文件: [root@centos redis-]# whereis ld ld: /usr/bin/ld.gold /usr/bin/ld /usr/bin/ld.bfd / ...

  3. python while 1 然后pass会发生什么?

    下面的代码,会使32核linux服务器志强e5的cpu使用率达到3200%,然后别的什么程序都跑不了. 如果windows,cpu使用率会达到100%.这是计算方式有点区别. 个人计算机,除非你是8路 ...

  4. 使用swig在python中调用C++

    1.安装swig 下载链接: http://www.swig.org/survey.html tar -xvf swig-.tar.gz ./configure --prefix=/usr/local ...

  5. Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢?

    Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢? 可视化图表,有相当多种,但常见的也就下面几种,其他比较复杂一点,大都也是基于如下几种进行组合,变换出来的.对于初学者来说,很容易被这官网上 ...

  6. Linux下tar的安装方式

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  7. python 解决 idea 或者 pucharm 编译器在引入自定义的 py 文件的类时,没有提示的问题

    1,问题描述: 假设我们有两个问价  a.py 和 b.py 其中分别有 A 类 和 B 类,现在我们想在 b.py 中引用 A 类 语法为 from a import A  虽然这是正确的语法,但是 ...

  8. ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开

    原因:修改归档模式的操作只能在 mount 状态下进行,不能处于 open 状态. SQL> alter database archivelog;alter database archivelo ...

  9. linux 安装 btrace

    1.下载btrace https://github.com/btraceio/btrace/releases/download/v1.3.8.3-1/btrace-bin-1.3.8.3.tgz2.上 ...

  10. Android 问题解决 HorizontalScrollView显示不全(转)

    原链接:https://www.jianshu.com/p/003adbcaff9d Android 问题解决 HorizontalScrollView显示不全 <HorizontalScrol ...