From fcdf565b1a7c75af8f488c349f6fdc1d23d4e654 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Fri, 15 Mar 2024 13:09:47 +0800 Subject: [PATCH] planner: output a SQL warning if binding loading is triggered (#51774) ref pingcap/tidb#51347 --- pkg/bindinfo/binding_cache.go | 6 ++- pkg/bindinfo/tests/timeout/BUILD.bazel | 1 + pkg/bindinfo/tests/timeout/timeout_test.go | 18 +++++++ .../r/planner/core/integration.result | 48 ------------------- .../t/planner/core/integration.test | 25 ---------- 5 files changed, 24 insertions(+), 74 deletions(-) diff --git a/pkg/bindinfo/binding_cache.go b/pkg/bindinfo/binding_cache.go index 3ddb253079b1e..7e3211665daa5 100644 --- a/pkg/bindinfo/binding_cache.go +++ b/pkg/bindinfo/binding_cache.go @@ -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) @@ -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)) } } } diff --git a/pkg/bindinfo/tests/timeout/BUILD.bazel b/pkg/bindinfo/tests/timeout/BUILD.bazel index 5bb372d94c3d9..b297862dca25e 100644 --- a/pkg/bindinfo/tests/timeout/BUILD.bazel +++ b/pkg/bindinfo/tests/timeout/BUILD.bazel @@ -12,6 +12,7 @@ go_test( "//pkg/bindinfo", "//pkg/testkit", "//pkg/testkit/testsetup", + "@com_github_stretchr_testify//require", "@org_uber_go_goleak//:goleak", ], ) diff --git a/pkg/bindinfo/tests/timeout/timeout_test.go b/pkg/bindinfo/tests/timeout/timeout_test.go index 7c8685b864ec9..5c390e5518790 100644 --- a/pkg/bindinfo/tests/timeout/timeout_test.go +++ b/pkg/bindinfo/tests/timeout/timeout_test.go @@ -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) diff --git a/tests/integrationtest/r/planner/core/integration.result b/tests/integrationtest/r/planner/core/integration.result index 89e11b5df3bf4..56aa43bc98f91 100644 --- a/tests/integrationtest/r/planner/core/integration.result +++ b/tests/integrationtest/r/planner/core/integration.result @@ -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 diff --git a/tests/integrationtest/t/planner/core/integration.test b/tests/integrationtest/t/planner/core/integration.test index e8ac4efeac49d..7dbd75208f0ab 100644 --- a/tests/integrationtest/t/planner/core/integration.test +++ b/tests/integrationtest/t/planner/core/integration.test @@ -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;