Skip to content

Commit

Permalink
statistics: change default value of tidb_analyze_version to 1 (#31826)
Browse files Browse the repository at this point in the history
close #31748
  • Loading branch information
yiwen92 authored Feb 10, 2022
1 parent c8a5e07 commit 094b3e5
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 34 deletions.
6 changes: 3 additions & 3 deletions cmd/explaintest/r/subquery.result
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Projection 5.00 root Column#22
├─TableReader(Build) 5.00 root data:TableFullScan
│ └─TableFullScan 5.00 cop[tikv] table:t keep order:false
└─StreamAgg(Probe) 1.00 root funcs:count(1)->Column#21
└─IndexJoin 0.22 root inner join, inner:TableReader, outer key:test.t.a, inner key:test.t.a, equal cond:eq(test.t.a, test.t.a)
├─IndexReader(Build) 0.45 root index:IndexRangeScan
│ └─IndexRangeScan 0.45 cop[tikv] table:s, index:idx(b, c, d) range: decided by [eq(test.t.b, 1) eq(test.t.c, 1) eq(test.t.d, test.t.a)], keep order:false
└─IndexJoin 0.50 root inner join, inner:TableReader, outer key:test.t.a, inner key:test.t.a, equal cond:eq(test.t.a, test.t.a)
├─IndexReader(Build) 1.00 root index:IndexRangeScan
│ └─IndexRangeScan 1.00 cop[tikv] table:s, index:idx(b, c, d) range: decided by [eq(test.t.b, 1) eq(test.t.c, 1) eq(test.t.d, test.t.a)], keep order:false
└─TableReader(Probe) 1.00 root data:TableRangeScan
└─TableRangeScan 1.00 cop[tikv] table:t1 range: decided by [test.t.a], keep order:false
drop table if exists t;
Expand Down
18 changes: 10 additions & 8 deletions executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ func (s *testFastAnalyze) TestFastAnalyze(c *C) {
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key, b int, c char(10), index index_b(b))")
tk.MustExec("set @@session.tidb_enable_fast_analyze=1")
tk.MustExec("set @@session.tidb_build_stats_concurrency=1")
tk.MustExec("set @@session.tidb_enable_fast_analyze = 1")
tk.MustExec("set @@session.tidb_build_stats_concurrency = 1")
tk.MustExec("set @@tidb_analyze_version = 1")
// Should not panic.
tk.MustExec("analyze table t")
Expand Down Expand Up @@ -564,18 +564,18 @@ func (s *testSerialSuite2) TestAnalyzeIndex(c *C) {
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (id int, v int, primary key(id), index k(v))")
tk.MustExec("insert into t1(id, v) values(1, 2), (2, 2), (3, 2), (4, 2), (5, 1), (6, 3), (7, 4)")
tk.MustExec("set @@tidb_analyze_version=1")
tk.MustExec("set @@tidb_analyze_version = 1")
tk.MustExec("analyze table t1 index k")
c.Assert(len(tk.MustQuery("show stats_buckets where table_name = 't1' and column_name = 'k' and is_index = 1").Rows()), Greater, 0)
tk.MustExec("set @@tidb_analyze_version=default")
tk.MustExec("analyze table t1")
c.Assert(len(tk.MustQuery("show stats_topn where table_name = 't1' and column_name = 'k' and is_index = 1").Rows()), Greater, 0)
c.Assert(len(tk.MustQuery("show stats_topn where table_name = 't1' and column_name = 'k' and is_index = 1").Rows()), Equals, 0)

func() {
defer tk.MustExec("set @@session.tidb_enable_fast_analyze=0")
tk.MustExec("drop stats t1")
tk.MustExec("set @@session.tidb_enable_fast_analyze=1")
tk.MustExec("set @@tidb_analyze_version=1")
tk.MustExec("set @@session.tidb_enable_fast_analyze = 1")
tk.MustExec("set @@tidb_analyze_version = 1")
tk.MustExec("analyze table t1 index k")
c.Assert(len(tk.MustQuery("show stats_buckets where table_name = 't1' and column_name = 'k' and is_index = 1").Rows()), Greater, 1)
}()
Expand Down Expand Up @@ -863,7 +863,7 @@ func (s *testSuite1) TestNormalAnalyzeOnCommonHandle(c *C) {
tk.MustExec("insert into t3 values(1,1,1), (2,2,2), (3,3,3)")

// Version2 is tested in TestStatsVer2.
tk.MustExec("set@@tidb_analyze_version=1")
tk.MustExec("set@@tidb_analyze_version = 1")
tk.MustExec("analyze table t1, t2, t3")

tk.MustQuery(`show stats_buckets where table_name in ("t1", "t2", "t3")`).Sort().Check(testkit.Rows(
Expand Down Expand Up @@ -913,6 +913,8 @@ func (s *testSuite1) TestDefaultValForAnalyze(c *C) {
for i := 1; i < 4; i++ {
tk.MustExec("insert into t values (?)", i)
}
tk.MustQuery("select @@tidb_analyze_version").Check(testkit.Rows("1"))
tk.MustQuery("select @@session.tidb_analyze_version").Check(testkit.Rows("1"))
tk.MustQuery("select @@tidb_enable_fast_analyze").Check(testkit.Rows("0"))
tk.MustQuery("select @@session.tidb_enable_fast_analyze").Check(testkit.Rows("0"))
tk.MustExec("analyze table t with 0 topn;")
Expand Down Expand Up @@ -956,7 +958,7 @@ func (s *testSerialSuite2) TestIssue20874(c *C) {
tk.MustExec("delete from mysql.stats_histograms")
tk.MustExec("create table t (a char(10) collate utf8mb4_unicode_ci not null, b char(20) collate utf8mb4_general_ci not null, key idxa(a), key idxb(b))")
tk.MustExec("insert into t values ('#', 'C'), ('$', 'c'), ('a', 'a')")
tk.MustExec("set @@tidb_analyze_version=1")
tk.MustExec("set @@tidb_analyze_version = 1")
tk.MustExec("analyze table t")
tk.MustQuery("show stats_buckets where db_name = 'test' and table_name = 't'").Sort().Check(testkit.Rows(
"test t a 0 0 1 1 \x02\xd2 \x02\xd2 0",
Expand Down
4 changes: 2 additions & 2 deletions planner/core/testdata/analyze_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,14 @@
{
"Name": "TestAnalyze",
"Cases": [
"Analyze{Table(_tidb_rowid, a, b, _tidb_rowid)}",
"Analyze{Index(a),Table(a, b)}",
"TableReader(Table(t)->Sel([le(test.t.a, 2)]))",
"IndexReader(Index(t.b)[[-inf,2)])",
"TableReader(Table(t)->Sel([eq(test.t.a, 1) le(test.t.b, 2)]))",
"TableReader(Table(t1)->Sel([le(test.t1.a, 2)]))",
"IndexLookUp(Index(t1.a)[[1,1]], Table(t1)->Sel([le(test.t1.b, 2)]))",
"TableReader(Table(t2)->Sel([le(test.t2.a, 2)]))",
"Analyze{Table(_tidb_rowid, a, b, _tidb_rowid)}",
"Analyze{Index(a),Index(b)}",
"PartitionUnionAll{TableReader(Table(t4)->Sel([le(test.t4.a, 2)]))->TableReader(Table(t4)->Sel([le(test.t4.a, 2)]))}",
"PartitionUnionAll{IndexReader(Index(t4.b)[[-inf,2)])->IndexReader(Index(t4.b)[[-inf,2)])}",
"TableReader(Table(t4)->Sel([eq(test.t4.a, 1) le(test.t4.b, 2)]))"
Expand Down
4 changes: 2 additions & 2 deletions planner/core/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -984,8 +984,8 @@
{
"SQL": "select * from t1 where t1.a = 1 and t1.b < \"333\"",
"Plan": [
"TableReader 0.82 root data:TableRangeScan",
"└─TableRangeScan 0.82 cop[tikv] table:t1 range:[1 -inf,1 \"333\"), keep order:false"
"TableReader 0.67 root data:TableRangeScan",
"└─TableRangeScan 0.67 cop[tikv] table:t1 range:[1 -inf,1 \"333\"), keep order:false"
],
"Res": [
"1 111 1.1000000000 11"
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ const (
DefTiDBEnableAsyncCommit = false
DefTiDBEnable1PC = false
DefTiDBGuaranteeLinearizability = true
DefTiDBAnalyzeVersion = 2
DefTiDBAnalyzeVersion = 1
DefTiDBEnableIndexMergeJoin = false
DefTiDBTrackAggregateMemoryUsage = true
DefTiDBEnableExchangePartition = false
Expand Down
2 changes: 0 additions & 2 deletions statistics/handle/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ func (s *testStatsSuite) TestDDLHistogram(c *C) {
rs := testKit.MustQuery("select count(*) from mysql.stats_histograms where table_id = ? and hist_id = 1 and is_index =1", tableInfo.ID)
rs.Check(testkit.Rows("1"))
rs = testKit.MustQuery("select count(*) from mysql.stats_buckets where table_id = ? and hist_id = 1 and is_index = 1", tableInfo.ID)
rs.Check(testkit.Rows("0"))
rs = testKit.MustQuery("select count(*) from mysql.stats_top_n where table_id = ? and hist_id = 1 and is_index = 1", tableInfo.ID)
rs.Check(testkit.Rows("2"))
}

Expand Down
3 changes: 1 addition & 2 deletions statistics/handle/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,7 @@ func (s *testStatsSuite) TestAutoUpdate(c *C) {
hg, ok := stats.Indices[tableInfo.Indices[0].ID]
c.Assert(ok, IsTrue)
c.Assert(hg.NDV, Equals, int64(3))
c.Assert(hg.Len(), Equals, 0)
c.Assert(hg.TopN.Num(), Equals, 3)
c.Assert(hg.Len(), Equals, 3)
})
}

Expand Down
2 changes: 1 addition & 1 deletion statistics/testdata/stats_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@
},
{
"SQL": "select * from t where a < 8 and (b > 10 or c < 3 or b > 4) and a > 2",
"Selectivity": 0
"Selectivity": 0.3125
}
]
}
Expand Down
26 changes: 13 additions & 13 deletions util/ranger/testdata/ranger_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@
{
"SQL": "select * from t where a = 1 and ((b = 1) or (b = 2 and c = 3));",
"Plan": [
"TableReader_6 1.71 root data:TableRangeScan_5",
"└─TableRangeScan_5 1.71 cop[tikv] table:t range:[1 1,1 1], [1 2 3,1 2 3], keep order:false"
"TableReader_6 2.00 root data:TableRangeScan_5",
"└─TableRangeScan_5 2.00 cop[tikv] table:t range:[1 1,1 1], [1 2 3,1 2 3], keep order:false"
],
"Result": [
"1 1 1"
Expand All @@ -293,8 +293,8 @@
{
"SQL": "select * from t use index(primary) where ((a = 1) or (a = 2 and b = 2)) and c = 3;",
"Plan": [
"TableReader_7 0.68 root data:Selection_6",
"└─Selection_6 0.68 cop[tikv] eq(test.t.c, 3), or(eq(test.t.a, 1), and(eq(test.t.a, 2), eq(test.t.b, 2)))",
"TableReader_7 0.75 root data:Selection_6",
"└─Selection_6 0.75 cop[tikv] eq(test.t.c, 3), or(eq(test.t.a, 1), and(eq(test.t.a, 2), eq(test.t.b, 2)))",
" └─TableRangeScan_5 2.00 cop[tikv] table:t range:[1,1], [2,2], keep order:false"
],
"Result": [
Expand All @@ -304,7 +304,7 @@
{
"SQL": "select * from t where (a,b) in ((1,1),(2,2)) and c > 2 and (a,b,c) in ((1,1,1),(2,2,3));",
"Plan": [
"Selection_6 0.44 root gt(test.t.c, 2), or(and(eq(test.t.a, 1), eq(test.t.b, 1)), and(eq(test.t.a, 2), eq(test.t.b, 2)))",
"Selection_6 0.56 root gt(test.t.c, 2), or(and(eq(test.t.a, 1), eq(test.t.b, 1)), and(eq(test.t.a, 2), eq(test.t.b, 2)))",
"└─Batch_Point_Get_5 2.00 root table:t, clustered index:PRIMARY(a, b, c) keep order:false, desc:false"
],
"Result": [
Expand All @@ -314,8 +314,8 @@
{
"SQL": "select * from t where (a,b) in ((1,1),(2,2)) and c > 2;",
"Plan": [
"TableReader_6 1.19 root data:TableRangeScan_5",
"└─TableRangeScan_5 1.19 cop[tikv] table:t range:(1 1 2,1 1 +inf], (2 2 2,2 2 +inf], keep order:false"
"TableReader_6 1.00 root data:TableRangeScan_5",
"└─TableRangeScan_5 1.00 cop[tikv] table:t range:(1 1 2,1 1 +inf], (2 2 2,2 2 +inf], keep order:false"
],
"Result": [
"2 2 3"
Expand All @@ -324,8 +324,8 @@
{
"SQL": "select * from t where ((a = 1 and b = 1) or (a = 2 and b = 2)) and c > 2;",
"Plan": [
"TableReader_6 1.19 root data:TableRangeScan_5",
"└─TableRangeScan_5 1.19 cop[tikv] table:t range:(1 1 2,1 1 +inf], (2 2 2,2 2 +inf], keep order:false"
"TableReader_6 1.00 root data:TableRangeScan_5",
"└─TableRangeScan_5 1.00 cop[tikv] table:t range:(1 1 2,1 1 +inf], (2 2 2,2 2 +inf], keep order:false"
],
"Result": [
"2 2 3"
Expand Down Expand Up @@ -364,8 +364,8 @@
{
"SQL": "select * from t2 where t='aaaa';",
"Plan": [
"TableReader_7 1.00 root data:Selection_6",
"└─Selection_6 1.00 cop[tikv] eq(test.t2.t, \"aaaa\")",
"TableReader_7 0.00 root data:Selection_6",
"└─Selection_6 0.00 cop[tikv] eq(test.t2.t, \"aaaa\")",
" └─TableRangeScan_5 2.00 cop[tikv] table:t2 range:[0,+inf], keep order:false"
],
"Result": [
Expand All @@ -375,8 +375,8 @@
{
"SQL": "select * from t2 where t='aaaa' or t = 'a';",
"Plan": [
"TableReader_7 1.60 root data:Selection_6",
"└─Selection_6 1.60 cop[tikv] or(eq(test.t2.t, \"aaaa\"), eq(test.t2.t, \"a\"))",
"TableReader_7 0.80 root data:Selection_6",
"└─Selection_6 0.80 cop[tikv] or(eq(test.t2.t, \"aaaa\"), eq(test.t2.t, \"a\"))",
" └─TableRangeScan_5 2.00 cop[tikv] table:t2 range:[0,+inf], keep order:false"
],
"Result": [
Expand Down

0 comments on commit 094b3e5

Please sign in to comment.