Skip to content

Commit

Permalink
session variable: change default value of `tidb_remove_orderby_in_sub…
Browse files Browse the repository at this point in the history
…query` to true (#44164)

close #44163
  • Loading branch information
elsa0520 committed May 26, 2023
1 parent df91517 commit c8caa48
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 101 deletions.
54 changes: 27 additions & 27 deletions cmd/explaintest/r/cte.result
Expand Up @@ -417,15 +417,15 @@ c1 c1
// Sort should not exist, because we have order by in CTE definition. Not the best plan for now(#25674).
explain with cte1 as (select c1 from t1 order by c1) select /*+ MERGE_JOIN(dt1, dt2) */ * from t1 dt1 inner join cte1 dt2 on dt2.c1 = dt1.c1 order by 1, 2;
id estRows task access object operator info
MergeJoin_31 12487.50 root inner join, left key:test.t1.c1, right key:test.t1.c1
├─Sort_22(Build) 9990.00 root test.t1.c1
│ └─TableReader_28 9990.00 root data:Selection_27
│ └─Selection_27 9990.00 cop[tikv] not(isnull(test.t1.c1))
│ └─TableFullScan_26 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
└─Sort_21(Probe) 9990.00 root test.t1.c1
└─TableReader_20 9990.00 root data:Selection_19
└─Selection_19 9990.00 cop[tikv] not(isnull(test.t1.c1))
└─TableFullScan_18 10000.00 cop[tikv] table:dt1 keep order:false, stats:pseudo
MergeJoin_25 12487.50 root inner join, left key:test.t1.c1, right key:test.t1.c1
├─Sort_23(Build) 9990.00 root test.t1.c1
│ └─TableReader_22 9990.00 root data:Selection_21
│ └─Selection_21 9990.00 cop[tikv] not(isnull(test.t1.c1))
│ └─TableFullScan_20 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
└─Sort_19(Probe) 9990.00 root test.t1.c1
└─TableReader_18 9990.00 root data:Selection_17
└─Selection_17 9990.00 cop[tikv] not(isnull(test.t1.c1))
└─TableFullScan_16 10000.00 cop[tikv] table:dt1 keep order:false, stats:pseudo
with cte1 as (select c1 from t1 order by c1) select /*+ MERGE_JOIN(dt1, dt2) */ * from t1 dt1 inner join cte1 dt2 on dt2.c1 = dt1.c1 order by 1, 2;
c1 c1
1 1
Expand Down Expand Up @@ -486,15 +486,15 @@ c1 c1
// Expect Sort operator in CTE definition.
explain with cte1 as (select c1 from t1 order by c1) select /*+ MERGE_JOIN(dt1, dt2) */ * from t1 dt1 inner join cte1 dt2 on dt2.c1 = dt1.c1 order by 1, 2;
id estRows task access object operator info
MergeJoin_31 12487.50 root inner join, left key:test.t1.c1, right key:test.t1.c1
├─Sort_22(Build) 9990.00 root test.t1.c1
│ └─TableReader_28 9990.00 root data:Selection_27
│ └─Selection_27 9990.00 cop[tikv] not(isnull(test.t1.c1))
│ └─TableFullScan_26 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
└─Sort_21(Probe) 9990.00 root test.t1.c1
└─TableReader_20 9990.00 root data:Selection_19
└─Selection_19 9990.00 cop[tikv] not(isnull(test.t1.c1))
└─TableFullScan_18 10000.00 cop[tikv] table:dt1 keep order:false, stats:pseudo
MergeJoin_25 12487.50 root inner join, left key:test.t1.c1, right key:test.t1.c1
├─Sort_23(Build) 9990.00 root test.t1.c1
│ └─TableReader_22 9990.00 root data:Selection_21
│ └─Selection_21 9990.00 cop[tikv] not(isnull(test.t1.c1))
│ └─TableFullScan_20 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
└─Sort_19(Probe) 9990.00 root test.t1.c1
└─TableReader_18 9990.00 root data:Selection_17
└─Selection_17 9990.00 cop[tikv] not(isnull(test.t1.c1))
└─TableFullScan_16 10000.00 cop[tikv] table:dt1 keep order:false, stats:pseudo
with cte1 as (select c1 from t1 order by c1) select /*+ MERGE_JOIN(dt1, dt2) */ * from t1 dt1 inner join cte1 dt2 on dt2.c1 = dt1.c1 order by 1, 2;
c1 c1
1 1
Expand All @@ -508,13 +508,13 @@ c1 c1
// Sort should not exist, because tpk is ordered. Not the best plan for now(#25674).
explain with cte1 as (select c1 from tpk order by c1) select /*+ MERGE_JOIN(dt1, dt2) */ * from t1 dt1 inner join cte1 dt2 on dt2.c1 = dt1.c1 order by 1, 2;
id estRows task access object operator info
MergeJoin_33 12487.50 root inner join, left key:test.t1.c1, right key:test.tpk.c1
├─TableReader_29(Build) 10000.00 root data:TableFullScan_28
│ └─TableFullScan_28 10000.00 cop[tikv] table:tpk keep order:true, stats:pseudo
└─Sort_21(Probe) 9990.00 root test.t1.c1
└─TableReader_20 9990.00 root data:Selection_19
└─Selection_19 9990.00 cop[tikv] not(isnull(test.t1.c1))
└─TableFullScan_18 10000.00 cop[tikv] table:dt1 keep order:false, stats:pseudo
MergeJoin_23 12487.50 root inner join, left key:test.t1.c1, right key:test.tpk.c1
├─TableReader_21(Build) 10000.00 root data:TableFullScan_20
│ └─TableFullScan_20 10000.00 cop[tikv] table:tpk keep order:true, stats:pseudo
└─Sort_19(Probe) 9990.00 root test.t1.c1
└─TableReader_18 9990.00 root data:Selection_17
└─Selection_17 9990.00 cop[tikv] not(isnull(test.t1.c1))
└─TableFullScan_16 10000.00 cop[tikv] table:dt1 keep order:false, stats:pseudo
with cte1 as (select c1 from tpk order by c1) select /*+ MERGE_JOIN(dt1, dt2) */ * from t1 dt1 inner join cte1 dt2 on dt2.c1 = dt1.c1 order by 1, 2;
c1 c1
1 1
Expand Down Expand Up @@ -699,8 +699,8 @@ insert into t1 values (0), (1), (2), (3), (4);
create table t2 (a int);
insert into t2 values (1), (2), (3), (4), (5);
drop view if exists v1,v2;
create view v1 as with t1 as (select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc;
create view v2 as with recursive t1 as ( select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc;
create view v1 as with t1 as (select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc limit 5;
create view v2 as with recursive t1 as ( select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc limit 5;
create database if not exists test1;
use test1;
select * from test.v1;
Expand Down
5 changes: 2 additions & 3 deletions cmd/explaintest/r/explain_easy.result
Expand Up @@ -679,9 +679,8 @@ Sort 10000.00 root test.t.a, test.t.b
explain format = 'brief' select * from (select * from t order by c) t order by a, b;
id estRows task access object operator info
Sort 10000.00 root test.t.a, test.t.b
└─Sort 10000.00 root test.t.c
└─TableReader 10000.00 root data:TableFullScan
└─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
└─TableReader 10000.00 root data:TableFullScan
└─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
drop table if exists t;
set @@session.tidb_opt_insubq_to_join_and_agg=1;
explain format = 'brief' SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a;
Expand Down
7 changes: 3 additions & 4 deletions cmd/explaintest/r/index_merge.result
Expand Up @@ -455,10 +455,9 @@ c1 c2 c3
///// MEMORY Table
explain select count(c1) from (select /*+ use_index_merge(t_alias), stream_agg() */ count(1) c1 from information_schema.statements_summary where sum_latency >= 0 or max_latency >= 0 order by 1) dt;
id estRows task access object operator info
StreamAgg_10 1.00 root funcs:count(Column#96)->Column#97
└─Sort_11 1.00 root Column#96
└─StreamAgg_14 1.00 root funcs:count(1)->Column#96
└─MemTableScan_18 10000.00 root table:STATEMENTS_SUMMARY
StreamAgg_9 1.00 root funcs:count(Column#96)->Column#97
└─StreamAgg_11 1.00 root funcs:count(1)->Column#96
└─MemTableScan_15 10000.00 root table:STATEMENTS_SUMMARY
show warnings;
Level Code Message
select count(c1) from (select /*+ use_index_merge(t_alias), stream_agg() */ count(1) c1 from information_schema.statements_summary where sum_latency >= 0 or max_latency >= 0 order by 1) dt;
Expand Down
4 changes: 2 additions & 2 deletions cmd/explaintest/t/cte.test
Expand Up @@ -267,8 +267,8 @@ insert into t1 values (0), (1), (2), (3), (4);
create table t2 (a int);
insert into t2 values (1), (2), (3), (4), (5);
drop view if exists v1,v2;
create view v1 as with t1 as (select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc;
create view v2 as with recursive t1 as ( select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc;
create view v1 as with t1 as (select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc limit 5;
create view v2 as with recursive t1 as ( select a from t2 where t2.a=3 union select t2.a+1 from t1,t2 where t1.a=t2.a) select * from t1 order by a desc limit 5;
create database if not exists test1;
use test1;
select * from test.v1;
Expand Down
26 changes: 12 additions & 14 deletions executor/merge_join_test.go
Expand Up @@ -467,13 +467,12 @@ func TestMergeJoin(t *testing.T) {
tk.MustExec("create table s (a int)")
tk.MustExec("insert into s values (4), (1), (3), (2)")
tk.MustQuery("explain format = 'brief' select s1.a1 from (select a as a1 from s order by s.a desc) as s1 join (select a as a2 from s order by s.a desc) as s2 on s1.a1 = s2.a2 order by s1.a1 desc").Check(testkit.Rows(
"MergeJoin 12487.50 root inner join, left key:test.s.a, right key:test.s.a",
"├─Sort(Build) 9990.00 root test.s.a:desc",
"│ └─TableReader 9990.00 root data:Selection",
"│ └─Selection 9990.00 cop[tikv] not(isnull(test.s.a))",
"│ └─TableFullScan 10000.00 cop[tikv] table:s keep order:false, stats:pseudo",
"└─Sort(Probe) 9990.00 root test.s.a:desc",
" └─TableReader 9990.00 root data:Selection",
"Sort 12487.50 root test.s.a:desc",
"└─HashJoin 12487.50 root inner join, equal:[eq(test.s.a, test.s.a)]",
" ├─TableReader(Build) 9990.00 root data:Selection",
" │ └─Selection 9990.00 cop[tikv] not(isnull(test.s.a))",
" │ └─TableFullScan 10000.00 cop[tikv] table:s keep order:false, stats:pseudo",
" └─TableReader(Probe) 9990.00 root data:Selection",
" └─Selection 9990.00 cop[tikv] not(isnull(test.s.a))",
" └─TableFullScan 10000.00 cop[tikv] table:s keep order:false, stats:pseudo",
))
Expand Down Expand Up @@ -629,13 +628,12 @@ func TestShuffleMergeJoin(t *testing.T) {
tk.MustExec("create table s (a int)")
tk.MustExec("insert into s values (4), (1), (3), (2)")
tk.MustQuery("explain format = 'brief' select s1.a1 from (select a as a1 from s order by s.a desc) as s1 join (select a as a2 from s order by s.a desc) as s2 on s1.a1 = s2.a2 order by s1.a1 desc").Check(testkit.Rows(
"MergeJoin 12487.50 root inner join, left key:test.s.a, right key:test.s.a",
"├─Sort(Build) 9990.00 root test.s.a:desc",
"│ └─TableReader 9990.00 root data:Selection",
"│ └─Selection 9990.00 cop[tikv] not(isnull(test.s.a))",
"│ └─TableFullScan 10000.00 cop[tikv] table:s keep order:false, stats:pseudo",
"└─Sort(Probe) 9990.00 root test.s.a:desc",
" └─TableReader 9990.00 root data:Selection",
"Sort 12487.50 root test.s.a:desc",
"└─HashJoin 12487.50 root inner join, equal:[eq(test.s.a, test.s.a)]",
" ├─TableReader(Build) 9990.00 root data:Selection",
" │ └─Selection 9990.00 cop[tikv] not(isnull(test.s.a))",
" │ └─TableFullScan 10000.00 cop[tikv] table:s keep order:false, stats:pseudo",
" └─TableReader(Probe) 9990.00 root data:Selection",
" └─Selection 9990.00 cop[tikv] not(isnull(test.s.a))",
" └─TableFullScan 10000.00 cop[tikv] table:s keep order:false, stats:pseudo",
))
Expand Down
12 changes: 6 additions & 6 deletions executor/set_test.go
Expand Up @@ -684,12 +684,12 @@ func TestSetVar(t *testing.T) {
tk.MustQuery("select @@session.tidb_enable_new_cost_interface").Check(testkit.Rows("1"))

// test for tidb_remove_orderby_in_subquery
tk.MustQuery("select @@session.tidb_remove_orderby_in_subquery").Check(testkit.Rows("0")) // default value is 0
tk.MustExec("set session tidb_remove_orderby_in_subquery=1")
tk.MustQuery("select @@session.tidb_remove_orderby_in_subquery").Check(testkit.Rows("1"))
tk.MustQuery("select @@global.tidb_remove_orderby_in_subquery").Check(testkit.Rows("0")) // default value is 0
tk.MustExec("set global tidb_remove_orderby_in_subquery=1")
tk.MustQuery("select @@global.tidb_remove_orderby_in_subquery").Check(testkit.Rows("1"))
tk.MustQuery("select @@session.tidb_remove_orderby_in_subquery").Check(testkit.Rows("1")) // default value is 1
tk.MustExec("set session tidb_remove_orderby_in_subquery=0")
tk.MustQuery("select @@session.tidb_remove_orderby_in_subquery").Check(testkit.Rows("0"))
tk.MustQuery("select @@global.tidb_remove_orderby_in_subquery").Check(testkit.Rows("1")) // default value is 1
tk.MustExec("set global tidb_remove_orderby_in_subquery=0")
tk.MustQuery("select @@global.tidb_remove_orderby_in_subquery").Check(testkit.Rows("0"))

// test for tidb_opt_skew_distinct_agg
tk.MustQuery("select @@session.tidb_opt_skew_distinct_agg").Check(testkit.Rows("0")) // default value is 0
Expand Down
22 changes: 9 additions & 13 deletions planner/cascades/testdata/transformation_rules_suite_out.json
Expand Up @@ -6,32 +6,28 @@
"SQL": "select a, b from (select a, b from t as t1 order by a) as t2 where t2.b > 10",
"Result": [
"Group#0 Schema:[test.t.a,test.t.b]",
" Projection_5 input:[Group#1], test.t.a, test.t.b",
" Projection_4 input:[Group#1], test.t.a, test.t.b",
"Group#1 Schema:[test.t.a,test.t.b]",
" Sort_3 input:[Group#2], test.t.a",
" Projection_2 input:[Group#2], test.t.a, test.t.b",
"Group#2 Schema:[test.t.a,test.t.b]",
" Projection_2 input:[Group#3], test.t.a, test.t.b",
" TiKVSingleGather_7 input:[Group#3], table:t1",
"Group#3 Schema:[test.t.a,test.t.b]",
" TiKVSingleGather_8 input:[Group#4], table:t1",
" Selection_8 input:[Group#4], gt(test.t.b, 10)",
"Group#4 Schema:[test.t.a,test.t.b]",
" Selection_9 input:[Group#5], gt(test.t.b, 10)",
"Group#5 Schema:[test.t.a,test.t.b]",
" TableScan_7 table:t1, pk col:test.t.a"
" TableScan_6 table:t1, pk col:test.t.a"
]
},
{
"SQL": "select a, b from (select a, b from t as t1 order by a) as t2 where t2.a > 10",
"Result": [
"Group#0 Schema:[test.t.a,test.t.b]",
" Projection_5 input:[Group#1], test.t.a, test.t.b",
" Projection_4 input:[Group#1], test.t.a, test.t.b",
"Group#1 Schema:[test.t.a,test.t.b]",
" Sort_3 input:[Group#2], test.t.a",
" Projection_2 input:[Group#2], test.t.a, test.t.b",
"Group#2 Schema:[test.t.a,test.t.b]",
" Projection_2 input:[Group#3], test.t.a, test.t.b",
" TiKVSingleGather_7 input:[Group#3], table:t1",
"Group#3 Schema:[test.t.a,test.t.b]",
" TiKVSingleGather_8 input:[Group#4], table:t1",
"Group#4 Schema:[test.t.a,test.t.b]",
" TableScan_10 table:t1, pk col:test.t.a, cond:[gt(test.t.a, 10)]"
" TableScan_9 table:t1, pk col:test.t.a, cond:[gt(test.t.a, 10)]"
]
},
{
Expand Down
Expand Up @@ -2171,7 +2171,7 @@
},
{
"SQL": "select * from (select * from t use index() order by b) t left join t t1 on t.a=t1.a limit 10",
"Best": "IndexJoin{TableReader(Table(t)->TopN([test.t.b],0,10))->TopN([test.t.b],0,10)->TableReader(Table(t))}(test.t.a,test.t.a)->Limit"
"Best": "IndexJoin{TableReader(Table(t)->Limit)->Limit->TableReader(Table(t))}(test.t.a,test.t.a)->Limit"
},
{
"SQL": "select * from ((SELECT 1 a,3 b) UNION (SELECT 2,1) ORDER BY (SELECT 2)) t order by a,b",
Expand Down Expand Up @@ -2617,11 +2617,11 @@
},
{
"SQL": "select count(*) from (select * from t order by b) t group by b",
"Best": "TableReader(Table(t))->Sort->StreamAgg"
"Best": "TableReader(Table(t)->HashAgg)->HashAgg"
},
{
"SQL": "select count(*), x from (select b as bbb, a + 1 as x from (select * from t order by b) t) t group by bbb",
"Best": "TableReader(Table(t))->Sort->Projection->StreamAgg"
"Best": "TableReader(Table(t)->HashAgg)->HashAgg"
},
{
"SQL": "select sum(a), avg(b + c) from t group by d",
Expand Down

0 comments on commit c8caa48

Please sign in to comment.