pingcap/tiflow

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

開放

#12,182 建立於 2025年5月23日

 (0 則留言) (0 個反應) (0 位負責人)Go (310 個分叉)auto 404
good first issuetype/feature

倉庫指標

星標
 (461 顆星)
PR 合併指標
 (平均合併 5天) (30 天內合併 21 個 PR)

描述

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.

貢獻者指南