Skip to content

Commit

Permalink
planner: ignore lock for temporary table of PointGet and BatchPointGet (
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyj authored May 12, 2021
1 parent b1d134d commit e7db533
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
26 changes: 26 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3832,3 +3832,29 @@ func (s *testIntegrationSerialSuite) TestEnforceMPP(c *C) {
" └─Selection_20 10.00 285020.00 batchCop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_19 10000.00 255020.00 batchCop[tiflash] table:t keep order:false, stats:pseudo"))
}

func (s *testIntegrationSuite) TestEliminateLockForTemporaryTable(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test;")
tk.MustExec("create global temporary table t1 (a int primary key, b int, c int, index i_b(b)) on commit delete rows;")
defer func() {
tk.MustExec("drop global temporary table if exists t1;")
}()
tk.MustExec("begin;")
tk.MustExec("insert t1 values (8,8,9);")

var input []string
var output []struct {
SQL string
Plan []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain format = 'brief' " + tt).Rows())
})
tk.MustQuery("explain format = 'brief' " + tt).Check(testkit.Rows(output[i].Plan...))
}
}
25 changes: 25 additions & 0 deletions planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
Expand Down Expand Up @@ -186,6 +187,7 @@ func postOptimize(sctx sessionctx.Context, plan PhysicalPlan) PhysicalPlan {
plan = InjectExtraProjection(plan)
mergeContinuousSelections(plan)
plan = eliminateUnionScanAndLock(sctx, plan)
plan = eliminateLockForTemporaryTable(plan)
plan = enableParallelApply(sctx, plan)
return plan
}
Expand Down Expand Up @@ -322,6 +324,29 @@ func eliminateUnionScanAndLock(sctx sessionctx.Context, p PhysicalPlan) Physical
})
}

// eliminateLockForTemporaryTable eliminates lock for the temporary table.
func eliminateLockForTemporaryTable(p PhysicalPlan) PhysicalPlan {
iteratePhysicalPlan(p, func(p PhysicalPlan) bool {
if len(p.Children()) > 1 {
return false
}
switch x := p.(type) {
case *PointGetPlan:
if x.TblInfo.TempTableType != model.TempTableNone {
x.Lock = false
x.LockWaitTime = 0
}
case *BatchPointGetPlan:
if x.TblInfo.TempTableType != model.TempTableNone {
x.Lock = false
x.LockWaitTime = 0
}
}
return true
})
return p
}

func iteratePhysicalPlan(p PhysicalPlan, f func(p PhysicalPlan) bool) {
if !f(p) {
return
Expand Down
10 changes: 8 additions & 2 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,10 @@ func TryFastPlan(ctx sessionctx.Context, node ast.Node) (p Plan) {
if tidbutil.IsMemDB(fp.dbName) {
return nil
}
fp.Lock, fp.LockWaitTime = getLockWaitTime(ctx, x.LockInfo)
// ignore lock for temporary table.
if fp.TblInfo.TempTableType == model.TempTableNone {
fp.Lock, fp.LockWaitTime = getLockWaitTime(ctx, x.LockInfo)
}
p = fp
return
}
Expand All @@ -480,7 +483,10 @@ func TryFastPlan(ctx sessionctx.Context, node ast.Node) (p Plan) {
p = tableDual.Init(ctx, &property.StatsInfo{}, 0)
return
}
fp.Lock, fp.LockWaitTime = getLockWaitTime(ctx, x.LockInfo)
// ignore lock for temporary table.
if fp.TblInfo.TempTableType == model.TempTableNone {
fp.Lock, fp.LockWaitTime = getLockWaitTime(ctx, x.LockInfo)
}
p = fp
return
}
Expand Down
9 changes: 9 additions & 0 deletions planner/core/testdata/integration_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,14 @@
"select sum(1) from s1",
"select count(1) as cnt from s1 union select count(1) as cnt from s2"
]
},
{
"name": "TestEliminateLockForTemporaryTable",
"cases": [
"select * from t1 where a = 2 for update",
"select * from t1 where a in (1,2) for update",
"select c + 1 from t1 where a = 2 and c = 2 for update",
"select c + 1 from t1 where a in (1,2) and c = 2 for update"
]
}
]
33 changes: 33 additions & 0 deletions planner/core/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -1564,5 +1564,38 @@
]
}
]
},
{
"Name": "TestEliminateLockForTemporaryTable",
"Cases": [
{
"SQL": "select * from t1 where a = 2 for update",
"Plan": [
"Point_Get 1.00 root table:t1 handle:2"
]
},
{
"SQL": "select * from t1 where a in (1,2) for update",
"Plan": [
"Batch_Point_Get 2.00 root table:t1 handle:[1 2], keep order:false, desc:false"
]
},
{
"SQL": "select c + 1 from t1 where a = 2 and c = 2 for update",
"Plan": [
"Projection 0.00 root plus(test.t1.c, 1)->Column#4",
"└─Selection 0.00 root eq(test.t1.c, 2)",
" └─Point_Get 1.00 root table:t1 handle:2"
]
},
{
"SQL": "select c + 1 from t1 where a in (1,2) and c = 2 for update",
"Plan": [
"Projection 0.00 root plus(test.t1.c, 1)->Column#4",
"└─Selection 0.00 root eq(test.t1.c, 2)",
" └─Batch_Point_Get 2.00 root table:t1 handle:[1 2], keep order:false, desc:false"
]
}
]
}
]

0 comments on commit e7db533

Please sign in to comment.