Skip to content

Commit

Permalink
planner: forbid the optimizer to generate point-plan upon MVIndexes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored Jan 29, 2023
1 parent 4686338 commit 774d2eb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,9 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter
if canConvertPointGet && expression.MaybeOverOptimized4PlanCache(ds.ctx, path.AccessConds) {
canConvertPointGet = ds.canConvertToPointGetForPlanCache(path)
}
if canConvertPointGet && path.Index != nil && path.Index.MVIndex {
canConvertPointGet = false // cannot use PointGet upon MVIndex
}

if canConvertPointGet && !path.IsIntHandlePath {
// We simply do not build [batch] point get for prefix indexes. This can be optimized.
Expand Down
24 changes: 24 additions & 0 deletions planner/core/indexmerge_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package core_test

import (
"strings"
"testing"

"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/testdata"
"github.com/stretchr/testify/require"
)

func TestIndexMergeJSONMemberOf(t *testing.T) {
Expand Down Expand Up @@ -149,3 +151,25 @@ func TestMVIndexIndexMergePlanCache(t *testing.T) {
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}

func TestMVIndexPointGet(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t(j json, unique kj((cast(j as signed array))))`)

for _, sql := range []string{
"select j from t where j=1",
"select j from t where j=1 or j=2",
"select j from t where j in (1, 2)",
} {
plan := tk.MustQuery("explain " + sql).Rows()
hasPointGet := false
for _, line := range plan {
if strings.Contains(strings.ToLower(line[0].(string)), "point") {
hasPointGet = true
}
}
require.True(t, !hasPointGet) // no point-get plan
}
}
4 changes: 2 additions & 2 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ func newBatchPointGetPlan(
}
}
for _, idxInfo := range tbl.Indices {
if !idxInfo.Unique || idxInfo.State != model.StatePublic || idxInfo.Invisible ||
if !idxInfo.Unique || idxInfo.State != model.StatePublic || idxInfo.Invisible || idxInfo.MVIndex ||
!indexIsAvailableByHints(idxInfo, indexHints) {
continue
}
Expand Down Expand Up @@ -1099,7 +1099,7 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt, check bool
var err error

for _, idxInfo := range tbl.Indices {
if !idxInfo.Unique || idxInfo.State != model.StatePublic || idxInfo.Invisible ||
if !idxInfo.Unique || idxInfo.State != model.StatePublic || idxInfo.Invisible || idxInfo.MVIndex ||
!indexIsAvailableByHints(idxInfo, tblName.IndexHints) {
continue
}
Expand Down

0 comments on commit 774d2eb

Please sign in to comment.