MySql Binlog 更新语句 Where 子句包含所有 Table 列,即使客户端在 运行 查询时仅在 Where 子句中使用主键

MySql Binlog Update Statement Where Clause Has All Table Columns, Even If Client Uses Just Primary Key In Where Clause When Running Query

检查BinLog发现如下语句:它在where子句中有All table列。我传给mysql的query只有1个Column- Primary Key Column,那为什么log把所有的column都写到BinLog里了?我对 BinLog 执行此操作有一个特殊问题:这导致从 BinLog 恢复非常非常慢。我不是 MySQL DBA。请分享您对此主题的经验。

UPDATE `ctbmysql`.`ctm`
### WHERE
###   @1=1139195549890498825
###   @2=1138051383057521436
###   @3=1615397172
###   @4=''
###   @5=1130000000662993985
###   @6=113
###   @7=''
###   @8=''
###   @9=1635370236
###   @10='49.128.173.183'
###   @11='49.128.173.146'
###   @12=''
###   @13=''
###   @14=''
###   @15=0
###   @16=1
###   @17=''
###   @18=''
###   @19=''
###   @20=0
###   @21='google-play'
###   @22=4217121370623809752
###   @23=''
###   @24=1
###   @25=''
###   @26=''
###   @27=0
### SET
###   @1=1139195549890498825
###   @2=1138051383057521436
###   @3=1615397172
###   @4=''
###   @5=1130000000662993985
###   @6=113
###   @7=''
###   @8=''
###   @9=1635453015
###   @10='150.242.24.246'
###   @11='49.128.173.146'
###   @12=''
###   @13=''
###   @14=''
###   @15=0
###   @16=1
###   @17=''
###   @18=''
###   @19=''
###   @20=0
###   @21=''
###   @22=4217121370623809752
###   @23=''
###   @24=1
###   @25=''
###   @26=''
###   @27=0

MySQL 支持两种不同的 binary log formats 以及两者的混合:

  • STATEMENT causes logging to be statement based.
  • ROW causes logging to be row based. This is the default.
  • MIXED causes logging to use mixed format.

您可以通过设置 binlog_format 配置选项来选择一个。

您在日志中看到的是行格式。它列出要更新的行(及其所有值)。这应该是 而不是 的原始查询,它看起来很相似。例如。如果您的更新影响不止一行,它会为您的单个原始更新查询创建多个行更新。

这是预期的行为。来自 Advantages and Disadvantages of Statement-Based and Row-Based Replication:

RBR can generate more data that must be logged. To replicate a DML statement (such as an UPDATE or DELETE statement), statement-based replication writes only the statement to the binary log. By contrast, row-based replication writes each changed row to the binary log. If the statement changes many rows, row-based replication may write significantly more data to the binary log; this is true even for statements that are rolled back. This also means that making and restoring a backup can require more time.

然而,基于行的复制是默认的,原因如下:

Advantages of row-based replication:

  • All changes can be replicated. This is the safest form of replication.