pingcap/tiflow

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

Aperta

#12.182 aperta il 23 mag 2025

 (0 commenti) (0 reazioni) (0 assegnatari)Go (310 fork)auto 404
good first issuetype/feature

Metriche repository

Star
 (461 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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.

Guida contributor