pingcap/tiflow

DM: Remove WHERE columns and values in UPDATE SET if the values are the same

オープン

#12,182 opened on 2025/05/23

 (0 件のコメント) (0 件のリアクション) (0 人の担当者)Go (310 件のフォーク)auto 404
good first issuetype/feature

Repository metrics

Stars
 (461 個のスター)
PR merge metrics
 (PR metrics pending)

説明

Is your feature request related to a problem?

When DM is updating rows in TiDB during MySQL replication, it generates SQL for each row change like this:

UPDATE <table> SET <col1> = <val1>, <col2> = <val2>[...] WHERE <col1> = <val3>[,...] LIMIT 1

if the values in the WHERE clause is not changed (val1 == val3), then there is no need to include them in the SET clause.

So it would be better (shorter, less comparison on TiDB) to generate this instead:

UPDATE <table> SET <col2> = <val2>[...] WHERE <col1> = <val3>[,...] LIMIT 1

Describe the feature you'd like

I assume the code is here, and it would need some minor refactoring, to get the WHERE data first, and then compare to when generating the SET data.

Describe alternatives you've considered

Other enhancements would be to only update the values that has changed, not just the PK/Unique key, which would minimize the number of fields further and also simplify the code, something like:

if (r.preValues[i] == r.postValues[i]) {
  continue
}

This is probably the better solution, not sure how the actual compare between preValues and postValues would look like, since the types are []interface{}.

Teachability, Documentation, Adoption, Migration Strategy

This would result in a more compact and easier to read SQL, as well as possibly minor performance improvement on the TiDB side.

コントリビューターガイド