-
Notifications
You must be signed in to change notification settings - Fork 6k
executor: speed up replace into statement #7027
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
922d104
opt replace into
jackysp c79ef01
add comments
jackysp 9969613
Merge branch 'master' into replace
jackysp 3e8e4d3
need more dupKey row values
jackysp adbcbf3
address comments
jackysp 151198a
Merge branch 'master' into replace
zz-jason c6cf3ca
address comments
jackysp 3cb2720
address comments
jackysp 17ffd90
address comments
jackysp 8c1e3ab
Merge branch 'master' into replace
zimulala 81579aa
Merge branch 'master' into replace
coocood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ package executor | |
import ( | ||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/table/tables" | ||
"github.com/pingcap/tidb/tablecodec" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"golang.org/x/net/context" | ||
|
@@ -44,7 +46,56 @@ func (e *ReplaceExec) Open(ctx context.Context) error { | |
return nil | ||
} | ||
|
||
func (e *ReplaceExec) exec(rows []types.DatumRow) error { | ||
// removeRow removes the duplicate row and cleanup its keys in the key-value map, | ||
// but if the to-be-removed row equals to the to-be-added row, no remove or add things to do. | ||
func (e *ReplaceExec) removeRow(handle int64, newRow types.DatumRow) (bool, error) { | ||
oldRow, err := e.getOldRow(e.ctx, e.Table, handle) | ||
if err != nil { | ||
return false, errors.Trace(err) | ||
} | ||
rowUnchanged, err := types.EqualDatums(e.ctx.GetSessionVars().StmtCtx, oldRow, newRow) | ||
if err != nil { | ||
return false, errors.Trace(err) | ||
} | ||
if rowUnchanged { | ||
e.ctx.GetSessionVars().StmtCtx.AddAffectedRows(1) | ||
return true, nil | ||
} | ||
err = e.Table.RemoveRecord(e.ctx, handle, oldRow) | ||
if err != nil { | ||
return false, errors.Trace(err) | ||
} | ||
e.ctx.StmtAddDirtyTableOP(DirtyTableDeleteRow, e.Table.Meta().ID, handle, nil) | ||
e.ctx.GetSessionVars().StmtCtx.AddAffectedRows(1) | ||
|
||
// Cleanup keys map, because the record was removed. | ||
cleanupRows, err := e.getKeysNeedCheck(e.ctx, e.Table, []types.DatumRow{oldRow}) | ||
if err != nil { | ||
return false, errors.Trace(err) | ||
} | ||
if len(cleanupRows) > 0 { | ||
// The length of need-to-cleanup rows should be at most 1, due to we only input 1 row. | ||
e.deleteDupKeys(cleanupRows[0]) | ||
} | ||
return false, nil | ||
} | ||
|
||
// addRow adds a row when all the duplicate key were checked. | ||
func (e *ReplaceExec) addRow(row types.DatumRow) (int64, error) { | ||
// Set kv.PresumeKeyNotExists is safe here, because we've already removed all duplicated rows. | ||
e.ctx.Txn().SetOption(kv.PresumeKeyNotExists, nil) | ||
h, err := e.Table.AddRecord(e.ctx, row, false) | ||
e.ctx.Txn().DelOption(kv.PresumeKeyNotExists) | ||
if err != nil { | ||
return 0, errors.Trace(err) | ||
} | ||
if !e.ctx.GetSessionVars().ImportingData { | ||
e.ctx.StmtAddDirtyTableOP(DirtyTableAddRow, e.Table.Meta().ID, h, row) | ||
} | ||
return h, nil | ||
} | ||
|
||
func (e *ReplaceExec) exec(newRows []types.DatumRow) error { | ||
/* | ||
* MySQL uses the following algorithm for REPLACE (and LOAD DATA ... REPLACE): | ||
* 1. Try to insert the new row into the table | ||
|
@@ -57,46 +108,67 @@ func (e *ReplaceExec) exec(rows []types.DatumRow) error { | |
* because in this case, one row was inserted after the duplicate was deleted. | ||
* See http://dev.mysql.com/doc/refman/5.7/en/mysql-affected-rows.html | ||
*/ | ||
idx := 0 | ||
rowsLen := len(rows) | ||
sc := e.ctx.GetSessionVars().StmtCtx | ||
for { | ||
if idx >= rowsLen { | ||
err := e.batchGetInsertKeys(e.ctx, e.Table, newRows) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
// Batch get the to-be-replaced rows in storage. | ||
err = e.initDupOldRowValue(e.ctx, e.Table, newRows) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
for i, r := range e.toBeCheckedRows { | ||
for { | ||
|
||
rowUnchanged := false | ||
if r.handleKey != nil { | ||
if _, found := e.dupKVs[string(r.handleKey.newKV.key)]; found { | ||
handle, err := tablecodec.DecodeRowKey(r.handleKey.newKV.key) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
rowUnchanged, err = e.removeRow(handle, newRows[i]) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
if rowUnchanged { | ||
break | ||
} | ||
continue | ||
} | ||
} | ||
|
||
foundDupKey := false | ||
for _, uk := range r.uniqueKeys { | ||
if val, found := e.dupKVs[string(uk.newKV.key)]; found { | ||
handle, err := tables.DecodeHandle(val) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
rowUnchanged, err = e.removeRow(handle, newRows[i]) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
foundDupKey = true | ||
break | ||
} | ||
} | ||
if rowUnchanged { | ||
break | ||
} | ||
if foundDupKey { | ||
continue | ||
} | ||
// No duplicated rows now, insert the row and go to the next row. | ||
newHandle, err := e.addRow(newRows[i]) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
e.fillBackKeys(e.Table, r, newHandle) | ||
break | ||
} | ||
row := rows[idx] | ||
h, err1 := e.Table.AddRecord(e.ctx, row, false) | ||
if err1 == nil { | ||
e.ctx.StmtAddDirtyTableOP(DirtyTableAddRow, e.Table.Meta().ID, h, row) | ||
idx++ | ||
continue | ||
} | ||
if err1 != nil && !kv.ErrKeyExists.Equal(err1) { | ||
return errors.Trace(err1) | ||
} | ||
oldRow, err1 := e.Table.Row(e.ctx, h) | ||
if err1 != nil { | ||
return errors.Trace(err1) | ||
} | ||
rowUnchanged, err1 := types.EqualDatums(sc, oldRow, row) | ||
if err1 != nil { | ||
return errors.Trace(err1) | ||
} | ||
if rowUnchanged { | ||
// If row unchanged, we do not need to do insert. | ||
e.ctx.GetSessionVars().StmtCtx.AddAffectedRows(1) | ||
idx++ | ||
continue | ||
} | ||
// Remove current row and try replace again. | ||
err1 = e.Table.RemoveRecord(e.ctx, h, oldRow) | ||
if err1 != nil { | ||
return errors.Trace(err1) | ||
} | ||
e.ctx.StmtAddDirtyTableOP(DirtyTableDeleteRow, e.Table.Meta().ID, h, nil) | ||
e.ctx.GetSessionVars().StmtCtx.AddAffectedRows(1) | ||
} | ||
|
||
if e.lastInsertID != 0 { | ||
e.ctx.GetSessionVars().SetLastInsertID(e.lastInsertID) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the row is unchanged, we don't need to remove the row and insert it again.