Skip to content

Commit

Permalink
planner: add Fix52592 control to disallow fast path (#56097)
Browse files Browse the repository at this point in the history
close #52592
  • Loading branch information
dbsid authored Sep 26, 2024
1 parent 7c88876 commit ba90793
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,9 @@ func findBestTask4DS(ds *DataSource, prop *property.PhysicalProperty, planCounte
}

canConvertPointGet := len(path.Ranges) > 0 && path.StoreType == kv.TiKV && isPointGetConvertableSchema(ds)
if fixcontrol.GetBoolWithDefault(ds.SCtx().GetSessionVars().OptimizerFixControl, fixcontrol.Fix52592, false) {
canConvertPointGet = false
}

if canConvertPointGet && path.Index != nil && path.Index.MVIndex {
canConvertPointGet = false // cannot use PointGet upon MVIndex
Expand Down
4 changes: 3 additions & 1 deletion pkg/planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/pingcap/tidb/pkg/planner/property"
"github.com/pingcap/tidb/pkg/planner/util"
"github.com/pingcap/tidb/pkg/planner/util/costusage"
"github.com/pingcap/tidb/pkg/planner/util/fixcontrol"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
"github.com/pingcap/tidb/pkg/privilege"
"github.com/pingcap/tidb/pkg/sessionctx"
Expand Down Expand Up @@ -883,8 +884,9 @@ type PointPlanVal struct {

// TryFastPlan tries to use the PointGetPlan for the query.
func TryFastPlan(ctx base.PlanContext, node *resolve.NodeW) (p base.Plan) {
if checkStableResultMode(ctx) {
if checkStableResultMode(ctx) || fixcontrol.GetBoolWithDefault(ctx.GetSessionVars().OptimizerFixControl, fixcontrol.Fix52592, false) {
// the rule of stabilizing results has not taken effect yet, so cannot generate a plan here in this mode
// or Fix52592 is turn on to disable fast path for select, update and delete
return nil
}

Expand Down
49 changes: 49 additions & 0 deletions pkg/planner/core/point_get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,52 @@ func TestIssue18042(t *testing.T) {
require.Equal(t, uint64(100), tk.Session().GetSessionVars().StmtCtx.MaxExecutionTime)
tk.MustExec("drop table t")
}

func TestIssue52592(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`set @@tidb_opt_fix_control = "52592:OFF"`) // affect hit counter in this ut
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a bigint unsigned primary key, b int, c int, key idx_bc(b,c))")
tk.MustExec("insert into t values(1, 1, 1), (2, 2, 2), (3, 3, 3)")
tk.MustQuery("explain format = 'brief' select * from t where a = 1").Check(testkit.Rows(
"Point_Get 1.00 root table:t handle:1",
))
tk.MustQuery("explain format = 'brief' select * from t where 1 = a").Check(testkit.Rows(
"Point_Get 1.00 root table:t handle:1",
))
tk.MustQuery("explain format = 'brief' update t set b=b+1, c=c+1 where a = 1").Check(testkit.Rows(
"Update N/A root N/A",
"└─Point_Get 1.00 root table:t handle:1",
))
tk.MustQuery("explain format = 'brief' delete from t where a = 1").Check(testkit.Rows(
"Delete N/A root N/A",
"└─Point_Get 1.00 root table:t handle:1",
))
tk.MustQuery("explain format = 'brief' select a from t where a = -1").Check(testkit.Rows(
"TableDual 0.00 root rows:0",
))
tk.MustExec(`set @@tidb_opt_fix_control = "52592:ON"`)
tk.MustQuery("explain format = 'brief' select * from t where a = 1").Check(testkit.Rows(
"TableReader 1.00 root data:TableRangeScan",
"└─TableRangeScan 1.00 cop[tikv] table:t range:[1,1], keep order:false, stats:pseudo",
))
tk.MustQuery("explain format = 'brief' select * from t where 1 = a").Check(testkit.Rows(
"TableReader 1.00 root data:TableRangeScan",
"└─TableRangeScan 1.00 cop[tikv] table:t range:[1,1], keep order:false, stats:pseudo",
))
tk.MustQuery("explain format = 'brief' update t set b=b+1, c=c+1 where a = 1").Check(testkit.Rows(
"Update N/A root N/A",
"└─TableReader 1.00 root data:TableRangeScan",
" └─TableRangeScan 1.00 cop[tikv] table:t range:[1,1], keep order:false, stats:pseudo",
))
tk.MustQuery("explain format = 'brief' delete from t where a = 1").Check(testkit.Rows(
"Delete N/A root N/A",
"└─TableReader 1.00 root data:TableRangeScan",
" └─TableRangeScan 1.00 cop[tikv] table:t range:[1,1], keep order:false, stats:pseudo",
))
tk.MustQuery("explain format = 'brief' select a from t where a = -1").Check(testkit.Rows(
"TableDual 0.00 root rows:0",
))
}
10 changes: 10 additions & 0 deletions pkg/planner/util/fixcontrol/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ import (
)

const (
// Fix52592 controls whether to disallow fast path for Get and BatchGet.
// Fast path does not support column projection, so we need to disable it when column projection is required.
// Here are the common cases that column projection is required:
// 1. Wide tables with many columns, where applications only query a few columns.
// 2. JSON fields with large column values, where queries may not necessarily retrieve the values of JSON columns
// or only extract specific small JSON content.
// By default, fast path is enabled, when it's disabled, the optimizer will fallback to coproccessor.
// It should be only disabled when fast path is more expensive than coprocessor.
// See #52592
Fix52592 uint64 = 52592
// Fix33031 controls whether to disallow plan cache for partitioned
// tables (both prepared statments and non-prepared statements)
// See #33031
Expand Down

0 comments on commit ba90793

Please sign in to comment.