-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sinkv2 (ticdc): limit batch dml size #7960
Changes from 4 commits
5528c34
5fe81c5
fe8f888
1aba395
a7c087a
a92c641
047fc14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,72 +273,121 @@ func convertBinaryToString(row *model.RowChangedEvent) { | |
} | ||
} | ||
|
||
// TODO: Find a way to make batch delete dmls more efficient. | ||
func groupRowsByType( | ||
func (s *mysqlBackend) groupRowsByType( | ||
event *eventsink.TxnCallbackableEvent, | ||
tableInfo *timodel.TableInfo, | ||
spiltUpdate bool, | ||
) (insertRows, updateRows, deleteRows []*sqlmodel.RowChange) { | ||
) (insertRows, updateRows, deleteRows [][]*sqlmodel.RowChange) { | ||
preAllocateSize := len(event.Event.Rows) | ||
if preAllocateSize > s.cfg.MaxTxnRow { | ||
preAllocateSize = s.cfg.MaxTxnRow | ||
} | ||
|
||
insertRow := make([]*sqlmodel.RowChange, 0, preAllocateSize) | ||
updateRow := make([]*sqlmodel.RowChange, 0, preAllocateSize) | ||
deleteRow := make([]*sqlmodel.RowChange, 0, preAllocateSize) | ||
|
||
for _, row := range event.Event.Rows { | ||
convertBinaryToString(row) | ||
|
||
if row.IsInsert() { | ||
insertRows = append( | ||
insertRows, | ||
insertRow = append( | ||
insertRow, | ||
convert2RowChanges(row, tableInfo, sqlmodel.RowChangeInsert)) | ||
} else if row.IsDelete() { | ||
deleteRows = append( | ||
deleteRows, | ||
if len(insertRow) >= s.cfg.MaxTxnRow { | ||
insertRows = append(insertRows, insertRow) | ||
insertRow = make([]*sqlmodel.RowChange, 0, preAllocateSize) | ||
} | ||
} | ||
|
||
if row.IsDelete() { | ||
deleteRow = append( | ||
deleteRow, | ||
convert2RowChanges(row, tableInfo, sqlmodel.RowChangeDelete)) | ||
} else if row.IsUpdate() { | ||
if len(deleteRow) >= s.cfg.MaxTxnRow { | ||
deleteRows = append(deleteRows, deleteRow) | ||
deleteRow = make([]*sqlmodel.RowChange, 0, preAllocateSize) | ||
} | ||
} | ||
|
||
if row.IsUpdate() { | ||
if spiltUpdate { | ||
deleteRows = append( | ||
deleteRows, | ||
deleteRow = append( | ||
deleteRow, | ||
convert2RowChanges(row, tableInfo, sqlmodel.RowChangeDelete)) | ||
insertRows = append( | ||
insertRows, | ||
if len(deleteRow) >= s.cfg.MaxTxnRow { | ||
deleteRows = append(deleteRows, deleteRow) | ||
deleteRow = make([]*sqlmodel.RowChange, 0, s.cfg.MaxTxnRow) | ||
} | ||
insertRow = append( | ||
insertRow, | ||
convert2RowChanges(row, tableInfo, sqlmodel.RowChangeInsert)) | ||
if len(insertRow) >= s.cfg.MaxTxnRow { | ||
insertRows = append(insertRows, insertRow) | ||
insertRow = make([]*sqlmodel.RowChange, 0, s.cfg.MaxTxnRow) | ||
} | ||
} else { | ||
updateRows = append( | ||
updateRows, | ||
updateRow = append( | ||
updateRow, | ||
convert2RowChanges(row, tableInfo, sqlmodel.RowChangeUpdate)) | ||
if len(updateRow) >= s.cfg.MaxTxnRow { | ||
updateRows = append(updateRows, updateRow) | ||
updateRow = make([]*sqlmodel.RowChange, 0, s.cfg.MaxTxnRow) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the most time There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your reminder. |
||
} | ||
} | ||
} | ||
} | ||
|
||
if len(insertRow) > 0 { | ||
insertRows = append(insertRows, insertRow) | ||
} | ||
if len(updateRow) > 0 { | ||
updateRows = append(updateRows, updateRow) | ||
} | ||
if len(deleteRow) > 0 { | ||
deleteRows = append(deleteRows, deleteRow) | ||
} | ||
|
||
return | ||
} | ||
|
||
func batchSingleTxnDmls( | ||
func (s *mysqlBackend) batchSingleTxnDmls( | ||
event *eventsink.TxnCallbackableEvent, | ||
tableInfo *timodel.TableInfo, | ||
translateToInsert bool, | ||
) (sqls []string, values [][]interface{}) { | ||
insertRows, updateRows, deleteRows := groupRowsByType(event, tableInfo, !translateToInsert) | ||
insertRows, updateRows, deleteRows := s.groupRowsByType(event, tableInfo, !translateToInsert) | ||
|
||
if len(deleteRows) > 0 { | ||
sql, value := sqlmodel.GenDeleteSQL(deleteRows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
for _, rows := range deleteRows { | ||
sql, value := sqlmodel.GenDeleteSQL(rows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
} | ||
} | ||
|
||
// handle insert | ||
if len(insertRows) > 0 { | ||
if translateToInsert { | ||
sql, value := sqlmodel.GenInsertSQL(sqlmodel.DMLInsert, insertRows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
} else { | ||
sql, value := sqlmodel.GenInsertSQL(sqlmodel.DMLReplace, insertRows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
for _, rows := range insertRows { | ||
if translateToInsert { | ||
sql, value := sqlmodel.GenInsertSQL(sqlmodel.DMLInsert, rows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
} else { | ||
sql, value := sqlmodel.GenInsertSQL(sqlmodel.DMLReplace, rows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
} | ||
} | ||
} | ||
|
||
// handle update | ||
if len(updateRows) > 0 { | ||
// TODO: do a testing on update performance. | ||
sql, value := sqlmodel.GenUpdateSQL(updateRows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
for _, rows := range updateRows { | ||
sql, value := sqlmodel.GenUpdateSQL(rows...) | ||
sqls = append(sqls, sql) | ||
values = append(values, value) | ||
} | ||
} | ||
|
||
return | ||
|
@@ -417,7 +466,7 @@ func (s *mysqlBackend) prepareDMLs() *preparedDMLs { | |
if hasHandleKey(tableColumns) { | ||
// TODO(dongmen): find a better way to get table info. | ||
tableInfo := model.BuildTiDBTableInfo(tableColumns, firstRow.IndexColumns) | ||
sql, value := batchSingleTxnDmls(event, tableInfo, translateToInsert) | ||
sql, value := s.batchSingleTxnDmls(event, tableInfo, translateToInsert) | ||
sqls = append(sqls, sql...) | ||
values = append(values, value...) | ||
continue | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add a test case to verify the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, I will do it.