-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
planner: support the safe-update mode #20184
Changes from all commits
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 |
---|---|---|
|
@@ -655,6 +655,10 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter | |
t = invalidTask | ||
candidates := ds.skylinePruning(prop) | ||
|
||
svars := ds.ctx.GetSessionVars() | ||
safeUpdateMode := (svars.StmtCtx.InDeleteStmt || svars.StmtCtx.InUpdateStmt) && svars.EnableSafeUpdates | ||
numRangeScanPlans := 0 | ||
|
||
cntPlan = 0 | ||
for _, candidate := range candidates { | ||
path := candidate.path | ||
|
@@ -717,6 +721,9 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter | |
pointGetTask = ds.convertToBatchPointGet(prop, candidate) | ||
} | ||
if !pointGetTask.invalid() { | ||
if safeUpdateMode && notFullScanPlan(pointGetTask.plan()) { | ||
numRangeScanPlans++ | ||
} | ||
cntPlan += 1 | ||
planCounter.Dec(1) | ||
} | ||
|
@@ -741,6 +748,9 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter | |
return nil, 0, err | ||
} | ||
if !tblTask.invalid() { | ||
if safeUpdateMode && notFullScanPlan(tblTask.plan()) { | ||
numRangeScanPlans++ | ||
} | ||
cntPlan += 1 | ||
planCounter.Dec(1) | ||
} | ||
|
@@ -761,6 +771,9 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter | |
return nil, 0, err | ||
} | ||
if !idxTask.invalid() { | ||
if safeUpdateMode && notFullScanPlan(idxTask.plan()) { | ||
numRangeScanPlans++ | ||
} | ||
cntPlan += 1 | ||
planCounter.Dec(1) | ||
} | ||
|
@@ -772,9 +785,29 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter | |
} | ||
} | ||
|
||
if safeUpdateMode && numRangeScanPlans == 0 { | ||
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. Should we just check the 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. If the optimizer chooses a full-scan as the best plan, but actually some other range-scans can be used, we shouldn't return the error in this case. |
||
return nil, 0, ErrUpdateWithoutKeyInSafeMode | ||
} | ||
return | ||
} | ||
|
||
func notFullScanPlan(p PhysicalPlan) bool { | ||
if p == nil { | ||
return false | ||
} | ||
switch scan := p.(type) { | ||
case *PhysicalTableScan: | ||
return !scan.isFullScan() | ||
case *PhysicalIndexScan: | ||
return !scan.isFullScan() | ||
case *PhysicalIndexLookUpReader: | ||
return notFullScanPlan(scan.indexPlan) | ||
case *PointGetPlan, *BatchPointGetPlan: | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (ds *DataSource) convertToIndexMergeScan(prop *property.PhysicalProperty, candidate *candidatePath) (task task, err error) { | ||
if prop.TaskTp != property.RootTaskType || !prop.IsEmpty() { | ||
return invalidTask, nil | ||
|
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 we just compare with
ErrUpdateWithoutKeyInSafeMode
?