Skip to content

Commit

Permalink
use prop to pass expectCnt, add tests
Browse files Browse the repository at this point in the history
Signed-off-by: you06 <[email protected]>
  • Loading branch information
you06 committed Dec 23, 2021
1 parent 8c6794c commit a4d48ef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ func (ds *DataSource) convertToIndexScan(prop *property.PhysicalProperty, candid
indexPlan: is,
tblColHists: ds.TblColHists,
tblCols: ds.TblCols,
expectCnt: uint64(prop.ExpectedCnt),
}
cop.partitionInfo = PartitionInfo{
PruningConds: ds.allConds,
Expand Down
24 changes: 24 additions & 0 deletions planner/core/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,18 @@ func TestCopPaging(t *testing.T) {
" └─TableRowIDScan 1024.00 cop[tikv] table:t keep order:false"))
}

// selection between limit and indexlookup, limit 960 should also go paging
for i := 0; i < 10; i++ {
tk.MustQuery("explain format='brief' select * from t force index(i) where mod(id, 2) > 0 and id <= 1024 and c1 >= 0 and c1 <= 1024 and c2 in (2, 4, 6, 8) order by c1 limit 960").Check(kit.Rows(
"Limit 3.20 root offset:0, count:960",
"└─Selection 2.56 root gt(mod(test.t.id, 2), 0)",
" └─IndexLookUp 3.20 root paging:true",
" ├─Selection(Build) 819.20 cop[tikv] le(test.t.id, 1024)",
" │ └─IndexRangeScan 1024.00 cop[tikv] table:t, index:i(c1) range:[0,1024], keep order:true",
" └─Selection(Probe) 3.20 cop[tikv] in(test.t.c2, 2, 4, 6, 8)",
" └─TableRowIDScan 819.20 cop[tikv] table:t keep order:false"))
}

// limit 961 exceeds the threshold, it should not go paging
for i := 0; i < 10; i++ {
tk.MustQuery("explain format='brief' select * from t force index(i) where id <= 1024 and c1 >= 0 and c1 <= 1024 and c2 in (2, 4, 6, 8) order by c1 limit 961").Check(kit.Rows(
Expand All @@ -678,4 +690,16 @@ func TestCopPaging(t *testing.T) {
" └─Selection(Probe) 4.00 cop[tikv] in(test.t.c2, 2, 4, 6, 8)",
" └─TableRowIDScan 1024.00 cop[tikv] table:t keep order:false"))
}

// selection between limit and indexlookup, limit 961 should not go paging too
for i := 0; i < 10; i++ {
tk.MustQuery("explain format='brief' select * from t force index(i) where mod(id, 2) > 0 and id <= 1024 and c1 >= 0 and c1 <= 1024 and c2 in (2, 4, 6, 8) order by c1 limit 961").Check(kit.Rows(
"Limit 3.20 root offset:0, count:961",
"└─Selection 2.56 root gt(mod(test.t.id, 2), 0)",
" └─IndexLookUp 3.20 root ",
" ├─Selection(Build) 819.20 cop[tikv] le(test.t.id, 1024)",
" │ └─IndexRangeScan 1024.00 cop[tikv] table:t, index:i(c1) range:[0,1024], keep order:true",
" └─Selection(Probe) 3.20 cop[tikv] in(test.t.c2, 2, 4, 6, 8)",
" └─TableRowIDScan 819.20 cop[tikv] table:t keep order:false"))
}
}
3 changes: 2 additions & 1 deletion planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,8 @@ func buildIndexLookUpTask(ctx sessionctx.Context, t *copTask) *rootTask {
if ctx.GetSessionVars().EnablePaging && t.expectCnt > 0 && t.expectCnt <= paging.Threshold {
p.Paging = true
pagingCst := calcPagingCost(ctx, t)
// prevent enlarging the cost because we take paging as a better plan,
// if the cost is enlarged, it'll be easier to go another plan.
idxCst = math.Min(idxCst, pagingCst)
}
newTask.cst += idxCst
Expand Down Expand Up @@ -1190,7 +1192,6 @@ func (p *PhysicalLimit) attach2Task(tasks ...task) task {
pushedDownLimit.SetSchema(pushedDownLimit.children[0].Schema())
pushedDownLimit.cost = cop.cost()
}
cop.expectCnt = p.Count
t = cop.convertToRootTask(p.ctx)
sunk = p.sinkIntoIndexLookUp(t)
} else if mpp, ok := t.(*mppTask); ok {
Expand Down

0 comments on commit a4d48ef

Please sign in to comment.