Skip to content

Commit bfec732

Browse files
authored
statsitstics: avoid sync load column which is skiped type to analyze (#57144)
close #57138
1 parent 9ef5b59 commit bfec732

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

pkg/statistics/handle/syncload/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"//pkg/parser/mysql",
1515
"//pkg/sessionctx",
1616
"//pkg/sessionctx/stmtctx",
17+
"//pkg/sessionctx/variable",
1718
"//pkg/statistics",
1819
"//pkg/statistics/handle/storage",
1920
"//pkg/statistics/handle/types",
@@ -35,7 +36,7 @@ go_test(
3536
srcs = ["stats_syncload_test.go"],
3637
flaky = True,
3738
race = "on",
38-
shard_count = 5,
39+
shard_count = 6,
3940
deps = [
4041
":syncload",
4142
"//pkg/config",

pkg/statistics/handle/syncload/stats_syncload.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/pingcap/tidb/pkg/parser/mysql"
3131
"github.com/pingcap/tidb/pkg/sessionctx"
3232
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
33+
"github.com/pingcap/tidb/pkg/sessionctx/variable"
3334
"github.com/pingcap/tidb/pkg/statistics"
3435
"github.com/pingcap/tidb/pkg/statistics/handle/storage"
3536
statstypes "github.com/pingcap/tidb/pkg/statistics/handle/types"
@@ -300,6 +301,14 @@ func (s *statsSyncLoad) handleOneItemTask(task *statstypes.NeededItemTask) (err
300301
s.statsHandle.SPool().Put(se)
301302
}
302303
}()
304+
var skipTypes map[string]struct{}
305+
val, err := sctx.GetSessionVars().GlobalVarsAccessor.GetGlobalSysVar(variable.TiDBAnalyzeSkipColumnTypes)
306+
if err != nil {
307+
logutil.BgLogger().Warn("failed to get global variable", zap.Error(err))
308+
} else {
309+
skipTypes = variable.ParseAnalyzeSkipColumnTypes(val)
310+
}
311+
303312
item := task.Item.TableItemID
304313
tbl, ok := s.statsHandle.Get(item.TableID)
305314

@@ -335,6 +344,13 @@ func (s *statsSyncLoad) handleOneItemTask(task *statstypes.NeededItemTask) (err
335344
// so we have to get the column info from the domain.
336345
wrapper.colInfo = tblInfo.Meta().GetColumnByID(item.ID)
337346
}
347+
if skipTypes != nil {
348+
_, skip := skipTypes[types.TypeToStr(wrapper.colInfo.FieldType.GetType(), wrapper.colInfo.FieldType.GetCharset())]
349+
if skip {
350+
return nil
351+
}
352+
}
353+
338354
// If this column is not analyzed yet and we don't have it in memory.
339355
// We create a fake one for the pseudo estimation.
340356
if loadNeeded && !analyzed {
@@ -348,6 +364,7 @@ func (s *statsSyncLoad) handleOneItemTask(task *statstypes.NeededItemTask) (err
348364
return nil
349365
}
350366
}
367+
failpoint.Inject("handleOneItemTaskPanic", nil)
351368
t := time.Now()
352369
needUpdate := false
353370
wrapper, err = s.readStatsForOneItem(sctx, item, wrapper, isPkIsHandle, task.Item.FullLoad)

pkg/statistics/handle/syncload/stats_syncload_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ func TestSyncLoadSkipUnAnalyzedItems(t *testing.T) {
5555
failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/syncload/assertSyncLoadItems")
5656
}
5757

58+
func TestSyncLoadSkipAnalyzSkipColumnItems(t *testing.T) {
59+
store, dom := testkit.CreateMockStoreAndDomain(t)
60+
tk := testkit.NewTestKit(t, store)
61+
tk.MustExec("use test")
62+
tk.MustExec("drop table if exists t")
63+
tk.MustExec("create table t(`id` bigint(20) NOT NULL AUTO_INCREMENT,content text,PRIMARY KEY (`id`))")
64+
h := dom.StatsHandle()
65+
h.SetLease(1)
66+
67+
tk.MustExec("analyze table t")
68+
tk.MustExec("set @@session.tidb_analyze_skip_column_types = 'json, text, blob'") // text is not default.
69+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/syncload/handleOneItemTaskPanic", `panic`))
70+
tk.MustQuery("trace plan select * from t where content ='ab'")
71+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/syncload/handleOneItemTaskPanic"))
72+
}
73+
5874
func TestConcurrentLoadHist(t *testing.T) {
5975
store, dom := testkit.CreateMockStoreAndDomain(t)
6076

0 commit comments

Comments
 (0)