From c8caa483dbc57a80826859ab00aa916f823a8be7 Mon Sep 17 00:00:00 2001 From: Elsa <111482174+elsa0520@users.noreply.github.com> Date: Fri, 26 May 2023 11:31:39 +0800 Subject: [PATCH] session variable: change default value of `tidb_remove_orderby_in_subquery` to true (#44164) close pingcap/tidb#44163 --- cmd/explaintest/r/cte.result | 54 +++++++++---------- cmd/explaintest/r/explain_easy.result | 5 +- cmd/explaintest/r/index_merge.result | 7 ++- cmd/explaintest/t/cte.test | 4 +- executor/merge_join_test.go | 26 +++++---- executor/set_test.go | 12 ++--- .../transformation_rules_suite_out.json | 22 ++++---- .../testdata/plan_suite_out.json | 6 +-- .../testdata/integration_suite_out.json | 43 +++++++-------- planner/core/integration_test.go | 10 ++-- planner/core/physical_plan_test.go | 13 +++++ sessionctx/variable/tidb_vars.go | 2 +- 12 files changed, 103 insertions(+), 101 deletions(-) diff --git a/cmd/explaintest/r/cte.result b/cmd/explaintest/r/cte.result index 08f0a40543aac..0874ba1af13d3 100644 --- a/cmd/explaintest/r/cte.result +++ b/cmd/explaintest/r/cte.result @@ -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 @@ -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 @@ -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 @@ -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; diff --git a/cmd/explaintest/r/explain_easy.result b/cmd/explaintest/r/explain_easy.result index b737fada05f0e..a1397f1c4e84b 100644 --- a/cmd/explaintest/r/explain_easy.result +++ b/cmd/explaintest/r/explain_easy.result @@ -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; diff --git a/cmd/explaintest/r/index_merge.result b/cmd/explaintest/r/index_merge.result index b4ff1cc87e7e5..42d055b053abc 100644 --- a/cmd/explaintest/r/index_merge.result +++ b/cmd/explaintest/r/index_merge.result @@ -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; diff --git a/cmd/explaintest/t/cte.test b/cmd/explaintest/t/cte.test index 860317681a210..22d803314c6a6 100644 --- a/cmd/explaintest/t/cte.test +++ b/cmd/explaintest/t/cte.test @@ -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; diff --git a/executor/merge_join_test.go b/executor/merge_join_test.go index 509c4f948b9ed..e03e6baca51c2 100644 --- a/executor/merge_join_test.go +++ b/executor/merge_join_test.go @@ -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", )) @@ -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", )) diff --git a/executor/set_test.go b/executor/set_test.go index 2fcff1d33fe1a..457e47ca616bf 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -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 diff --git a/planner/cascades/testdata/transformation_rules_suite_out.json b/planner/cascades/testdata/transformation_rules_suite_out.json index d8f3ec0255e50..c567674e1e1c7 100644 --- a/planner/cascades/testdata/transformation_rules_suite_out.json +++ b/planner/cascades/testdata/transformation_rules_suite_out.json @@ -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)]" ] }, { diff --git a/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json b/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json index 3c2c09f88d9b7..66644f06195d9 100644 --- a/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json +++ b/planner/core/casetest/physicalplantest/testdata/plan_suite_out.json @@ -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", @@ -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", diff --git a/planner/core/casetest/testdata/integration_suite_out.json b/planner/core/casetest/testdata/integration_suite_out.json index 5e1a84ac99ae4..abde67b7d4cd1 100644 --- a/planner/core/casetest/testdata/integration_suite_out.json +++ b/planner/core/casetest/testdata/integration_suite_out.json @@ -10512,10 +10512,10 @@ " └─TableReader 8000.00 root MppVersion: 1, data:ExchangeSender", " └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough", " └─Projection 8000.00 mpp[tiflash] Column#5, test.t.id", - " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:sum(Column#7)->Column#5, funcs:firstrow(test.t.id)->test.t.id", + " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:sum(Column#9)->Column#5, funcs:firstrow(test.t.id)->test.t.id", " └─ExchangeReceiver 8000.00 mpp[tiflash] ", " └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.name, collate: utf8mb4_bin], [name: test.t.id, collate: binary]", - " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:count(1)->Column#7", + " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:count(1)->Column#9", " └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo" ] }, @@ -10526,10 +10526,10 @@ "└─TableReader 8000.00 root MppVersion: 1, data:ExchangeSender", " └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough", " └─Projection 8000.00 mpp[tiflash] Column#5", - " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.name, funcs:sum(Column#8)->Column#5", + " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.name, funcs:sum(Column#9)->Column#5", " └─ExchangeReceiver 8000.00 mpp[tiflash] ", " └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.name, collate: utf8mb4_bin]", - " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.name, funcs:count(1)->Column#8", + " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.name, funcs:count(1)->Column#9", " └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo" ] }, @@ -10541,8 +10541,8 @@ " └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough", " └─Projection 8000.00 mpp[tiflash] Column#5", " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.name, funcs:count(1)->Column#5", - " └─Projection 8000.00 mpp[tiflash] test.t.id, test.t.name", - " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:firstrow(test.t.id)->test.t.id, funcs:firstrow(test.t.name)->test.t.name", + " └─Projection 8000.00 mpp[tiflash] test.t.name", + " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:firstrow(test.t.name)->test.t.name", " └─ExchangeReceiver 8000.00 mpp[tiflash] ", " └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.name, collate: utf8mb4_bin]", " └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, ", @@ -10576,14 +10576,13 @@ "Sort 12487.50 root test.t.id", "└─TableReader 12487.50 root MppVersion: 1, data:ExchangeSender", " └─ExchangeSender 12487.50 mpp[tiflash] ExchangeType: PassThrough", - " └─Projection 12487.50 mpp[tiflash] test.t.id, test.t.value, test.t.name, test.t.id, test.t.value, test.t.name", - " └─HashJoin 12487.50 mpp[tiflash] inner join, equal:[eq(test.t.id, test.t.id)]", - " ├─ExchangeReceiver(Build) 9990.00 mpp[tiflash] ", - " │ └─ExchangeSender 9990.00 mpp[tiflash] ExchangeType: Broadcast, Compression: FAST", - " │ └─Selection 9990.00 mpp[tiflash] not(isnull(test.t.id))", - " │ └─TableFullScan 10000.00 mpp[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo", - " └─Selection(Probe) 9990.00 mpp[tiflash] not(isnull(test.t.id))", - " └─TableFullScan 10000.00 mpp[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo" + " └─HashJoin 12487.50 mpp[tiflash] inner join, equal:[eq(test.t.id, test.t.id)]", + " ├─ExchangeReceiver(Build) 9990.00 mpp[tiflash] ", + " │ └─ExchangeSender 9990.00 mpp[tiflash] ExchangeType: Broadcast, Compression: FAST", + " │ └─Selection 9990.00 mpp[tiflash] not(isnull(test.t.id))", + " │ └─TableFullScan 10000.00 mpp[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo", + " └─Selection(Probe) 9990.00 mpp[tiflash] not(isnull(test.t.id))", + " └─TableFullScan 10000.00 mpp[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo" ] }, { @@ -10606,15 +10605,13 @@ " └─Union 18000.00 mpp[tiflash] ", " ├─Projection 8000.00 mpp[tiflash] cast(Column#12, bigint(21) BINARY)->Column#12", " │ └─Projection 8000.00 mpp[tiflash] Column#5", - " │ └─Projection 8000.00 mpp[tiflash] Column#5, test.t.id", - " │ └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:sum(Column#19)->Column#5, funcs:firstrow(test.t.id)->test.t.id", - " │ └─ExchangeReceiver 8000.00 mpp[tiflash] ", - " │ └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.name, collate: utf8mb4_bin], [name: test.t.id, collate: binary]", - " │ └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:count(1)->Column#19", - " │ └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo", - " └─Projection 10000.00 mpp[tiflash] cast(Column#11, bigint(21) BINARY)->Column#12", - " └─Projection 10000.00 mpp[tiflash] plus(test.t.id, 1)->Column#11", - " └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo" + " │ └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:sum(Column#18)->Column#5", + " │ └─ExchangeReceiver 8000.00 mpp[tiflash] ", + " │ └─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.name, collate: utf8mb4_bin], [name: test.t.id, collate: binary]", + " │ └─HashAgg 8000.00 mpp[tiflash] group by:test.t.id, test.t.name, funcs:count(1)->Column#18", + " │ └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo", + " └─Projection 10000.00 mpp[tiflash] cast(plus(test.t.id, 1), bigint(21) BINARY)->Column#12", + " └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo" ] }, { diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index b6dfdd7b06403..6b9147fa5bfa8 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3050,11 +3050,11 @@ func TestSelectIgnoreTemporaryTableInView(t *testing.T) { tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost", CurrentUser: true, AuthUsername: "root", AuthHostname: "%"}, nil, []byte("012345678901234567890"), nil) tk.MustExec("create table t1 (a int, b int)") tk.MustExec("create table t2 (c int, d int)") - tk.MustExec("create view v1 as select * from t1 order by a") - tk.MustExec("create view v2 as select * from ((select * from t1) union (select * from t2)) as tt order by a, b") - tk.MustExec("create view v3 as select * from v1 order by a") - tk.MustExec("create view v4 as select * from t1, t2 where t1.a = t2.c order by a, b") - tk.MustExec("create view v5 as select * from (select * from t1) as t1 order by a") + tk.MustExec("create view v1 as select * from t1 order by a limit 5") + tk.MustExec("create view v2 as select * from ((select * from t1) union (select * from t2)) as tt order by a, b limit 5") + tk.MustExec("create view v3 as select * from v1 order by a limit 5") + tk.MustExec("create view v4 as select * from t1, t2 where t1.a = t2.c order by a, b limit 5") + tk.MustExec("create view v5 as select * from (select * from t1) as t1 order by a limit 5") tk.MustExec("insert into t1 values (1, 2), (3, 4)") tk.MustExec("insert into t2 values (3, 5), (6, 7)") diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index c93e0d7d6d6ab..9cc884bb18052 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -524,3 +524,16 @@ func TestPhysicalPlanMemoryTrace(t *testing.T) { pp.MPPPartitionCols = append(pp.MPPPartitionCols, &property.MPPPartitionColumn{}) require.Greater(t, pp.MemoryUsage(), size) } + +func TestRemoveOrderbyInSubquery(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + tk.MustExec("CREATE TABLE `t1` ( `a` int(11) DEFAULT NULL, `b` int(11) DEFAULT NULL, `c` int(11) DEFAULT NULL);") + tk.MustExec("insert into t1 (a,b,c) value(3,4,5);") + tk.MustQuery("explain format = 'brief' select * from (select * from t1 order by a) tmp;").Check( + testkit.Rows( + "TableReader 10000.00 root data:TableFullScan", + "└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo")) +} diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 64b3f86cf9dcb..ce71874506fcc 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -1217,7 +1217,7 @@ const ( DefTiDBIgnorePreparedCacheCloseStmt = false DefTiDBBatchPendingTiFlashCount = 4000 DefRCReadCheckTS = false - DefTiDBRemoveOrderbyInSubquery = false + DefTiDBRemoveOrderbyInSubquery = true DefTiDBSkewDistinctAgg = false DefTiDB3StageDistinctAgg = true DefTiDB3StageMultiDistinctAgg = false