Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: output a SQL warning if binding loading is triggered #51774

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unstable and duplicated with bindinfo/test/TestInvisibleIndex, just remove it.

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