Skip to content

Commit

Permalink
planner: output a SQL warning if binding loading is triggered (#51774)
Browse files Browse the repository at this point in the history
ref #51347
  • Loading branch information
qw4990 authored Mar 15, 2024
1 parent 3ab313d commit fcdf565
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 74 deletions.
6 changes: 5 additions & 1 deletion pkg/bindinfo/binding_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func (fbc *fuzzyBindingCache) loadFromStore(sctx sessionctx.Context, missingSQLD
if intest.InTest && sctx.Value(LoadBindingNothing) != nil {
return
}
defer func(start time.Time) {
sctx.GetSessionVars().StmtCtx.AppendWarning(errors.New("loading binding from storage takes " + time.Since(start).String()))
}(time.Now())

for _, sqlDigest := range missingSQLDigest {
start := time.Now()
bindings, err := fbc.loadBindingFromStorageFunc(sctx, sqlDigest)
Expand All @@ -173,7 +177,7 @@ func (fbc *fuzzyBindingCache) loadFromStore(sctx sessionctx.Context, missingSQLD
// When the memory capacity of bing_cache is not enough,
// there will be some memory-related errors in multiple places.
// Only needs to be handled once.
logutil.BindLogger().Warn("BindHandle.Update", zap.Error(err))
logutil.BindLogger().Warn("update binding cache error", zap.Error(err))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/bindinfo/tests/timeout/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_test(
"//pkg/bindinfo",
"//pkg/testkit",
"//pkg/testkit/testsetup",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
],
)
18 changes: 18 additions & 0 deletions pkg/bindinfo/tests/timeout/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,26 @@ import (

"github.com/pingcap/tidb/pkg/bindinfo"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/stretchr/testify/require"
)

func TestLoadBindingWarn(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t1 (a int)`)
tk.MustExec(`create global binding using select * from t1`)
tk.MustExec(`select * from t1`)
tk.MustQuery(`show warnings`).Check(testkit.Rows()) // no warning

sctx := tk.Session()
sctx.SetValue(bindinfo.GetBindingReturnNilAlways, true)
tk.MustExec(`select * from t1`)
warnings := tk.MustQuery(`show warnings`)
require.True(t, len(warnings.Rows()) == 1)
sctx.ClearValue(bindinfo.GetBindingReturnNilAlways)
}

func TestFuzzyBindingHintsWithSourceReturningTimeout(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
48 changes: 0 additions & 48 deletions tests/integrationtest/r/planner/core/integration.result
Original file line number Diff line number Diff line change
Expand Up @@ -305,54 +305,6 @@ c2 c0
0 0
1 1
0 1
drop table if exists t;
create table t(a int, b int, unique index i_a (a) invisible, unique index i_b(b));
insert into t values (1,2);
admin check table t;
select a from t order by a;
a
1
explain select a from t order by a;
id estRows task access object operator info
Sort_4 10000.00 root planner__core__integration.t.a
└─TableReader_8 10000.00 root data:TableFullScan_7
└─TableFullScan_7 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
select a from t where a > 0;
a
1
explain select a from t where a > 1;
id estRows task access object operator info
TableReader_7 3333.33 root data:Selection_6
└─Selection_6 3333.33 cop[tikv] gt(planner__core__integration.t.a, 1)
└─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
select * from t use index(i_a);
Error 1176 (42000): Key 'i_a' doesn't exist in table 't'
select * from t force index(i_a);
Error 1176 (42000): Key 'i_a' doesn't exist in table 't'
select * from t ignore index(i_a);
Error 1176 (42000): Key 'i_a' doesn't exist in table 't'
select /*+ USE_INDEX(t, i_a) */ * from t;
a b
1 2
Level Code Message
Warning 1176 Key 'i_a' doesn't exist in table 't'
select /*+ IGNORE_INDEX(t, i_a), USE_INDEX(t, i_b) */ a from t order by a;
a
1
Level Code Message
Warning 1176 Key 'i_a' doesn't exist in table 't'
select /*+ FORCE_INDEX(t, i_a), USE_INDEX(t, i_b) */ a from t order by a;
a
1
Level Code Message
Warning 1176 Key 'i_a' doesn't exist in table 't'
select /*+ FORCE_INDEX(aaa) */ * from t;
a b
1 2
Level Code Message
Warning 1815 force_index(planner__core__integration.aaa) is inapplicable, check whether the table(planner__core__integration.aaa) exists
admin check table t;
admin check index t i_a;
select max(t.col) from (select 'a' as col union all select '' as col) as t;
max(t.col)
a
Expand Down
25 changes: 0 additions & 25 deletions tests/integrationtest/t/planner/core/integration.test
Original file line number Diff line number Diff line change
Expand Up @@ -234,31 +234,6 @@ INSERT INTO t0(c0) VALUES (3);
SELECT /*+ MERGE_JOIN(t1, t0, v0)*/v0.c2, t1.c0 FROM v0, t0 CROSS JOIN t1 ORDER BY -v0.c1;


# TestInvisibleIndex
drop table if exists t;
create table t(a int, b int, unique index i_a (a) invisible, unique index i_b(b));
insert into t values (1,2);
admin check table t;
select a from t order by a;
explain select a from t order by a;
select a from t where a > 0;
explain select a from t where a > 1;
--error 1176
select * from t use index(i_a);
--error 1176
select * from t force index(i_a);
--error 1176
select * from t ignore index(i_a);
--enable_warnings
select /*+ USE_INDEX(t, i_a) */ * from t;
select /*+ IGNORE_INDEX(t, i_a), USE_INDEX(t, i_b) */ a from t order by a;
select /*+ FORCE_INDEX(t, i_a), USE_INDEX(t, i_b) */ a from t order by a;
select /*+ FORCE_INDEX(aaa) */ * from t;
--disable_warnings
admin check table t;
admin check index t i_a;


# TestTopNByConstFunc
select max(t.col) from (select 'a' as col union all select '' as col) as t;

Expand Down

0 comments on commit fcdf565

Please sign in to comment.