Skip to content

Commit 5350e79

Browse files
authored
statsitstics: avoid sync load column which is skiped type to analyze (#57144) (#57615)
close #57138
1 parent 58c3099 commit 5350e79

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",
@@ -34,7 +35,7 @@ go_test(
3435
srcs = ["stats_syncload_test.go"],
3536
flaky = True,
3637
race = "on",
37-
shard_count = 7,
38+
shard_count = 8,
3839
deps = [
3940
":syncload",
4041
"//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"
@@ -306,6 +307,14 @@ func (s *statsSyncLoad) handleOneItemTask(task *statstypes.NeededItemTask) (err
306307
s.statsHandle.SPool().Destroy(se)
307308
}
308309
}()
310+
var skipTypes map[string]struct{}
311+
val, err := sctx.GetSessionVars().GlobalVarsAccessor.GetGlobalSysVar(variable.TiDBAnalyzeSkipColumnTypes)
312+
if err != nil {
313+
logutil.BgLogger().Warn("failed to get global variable", zap.Error(err))
314+
} else {
315+
skipTypes = variable.ParseAnalyzeSkipColumnTypes(val)
316+
}
317+
309318
item := task.Item.TableItemID
310319
statsTbl, ok := s.statsHandle.Get(item.TableID)
311320

@@ -342,6 +351,13 @@ func (s *statsSyncLoad) handleOneItemTask(task *statstypes.NeededItemTask) (err
342351
// so we have to get the column info from the domain.
343352
wrapper.colInfo = tblInfo.GetColumnByID(item.ID)
344353
}
354+
if skipTypes != nil {
355+
_, skip := skipTypes[types.TypeToStr(wrapper.colInfo.FieldType.GetType(), wrapper.colInfo.FieldType.GetCharset())]
356+
if skip {
357+
return nil
358+
}
359+
}
360+
345361
// If this column is not analyzed yet and we don't have it in memory.
346362
// We create a fake one for the pseudo estimation.
347363
// Otherwise, it will trigger the sync/async load again, even if the column has not been analyzed.
@@ -351,6 +367,7 @@ func (s *statsSyncLoad) handleOneItemTask(task *statstypes.NeededItemTask) (err
351367
return nil
352368
}
353369
}
370+
failpoint.Inject("handleOneItemTaskPanic", nil)
354371
t := time.Now()
355372
needUpdate := false
356373
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
@@ -56,6 +56,22 @@ func TestSyncLoadSkipUnAnalyzedItems(t *testing.T) {
5656
failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/syncload/assertSyncLoadItems")
5757
}
5858

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

0 commit comments

Comments
 (0)