From 2c2d52bc1a01cdf22b4263629f9374a9ef5aa33b Mon Sep 17 00:00:00 2001 From: nitao Date: Fri, 13 Sep 2024 00:53:56 +0800 Subject: [PATCH] support order by limit optimization for non-sorted columns (#18753) support order by limit optimization for non-sorted columns Approved by: @heni02, @aressu1985, @ouyuanning --- pkg/sql/plan/message.go | 3 --- test/distributed/cases/optimizer/top.result | 22 +++++++++++++++++++++ test/distributed/cases/optimizer/top.test | 3 +++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pkg/sql/plan/message.go b/pkg/sql/plan/message.go index 82f71a65b9d8..8b4d7f1b8e67 100644 --- a/pkg/sql/plan/message.go +++ b/pkg/sql/plan/message.go @@ -78,9 +78,6 @@ func (builder *QueryBuilder) handleMessgaeFromTopToScan(nodeID int32) { if orderByCol.Col.RelPos != scanNode.BindingTags[0] { return } - if GetSortOrder(scanNode.TableDef, orderByCol.Col.ColPos) != 0 { - return - } msgTag := builder.genNewMsgTag() msgHeader := &plan.MsgHeader{MsgTag: msgTag, MsgType: int32(message.MsgTopValue)} diff --git a/test/distributed/cases/optimizer/top.result b/test/distributed/cases/optimizer/top.result index b25276e52bd2..4ebc763b294a 100644 --- a/test/distributed/cases/optimizer/top.result +++ b/test/distributed/cases/optimizer/top.result @@ -172,6 +172,28 @@ c1 c2 c3 c1 c2 c3 899983 899983 899983 899983 899983 899983 899982 899982 899982 899982 899982 899982 899981 899981 899981 899981 899981 899981 +explain select * from t1 order by c2 limit 10 offset 20; +AP QUERY PLAN ON MULTICN(4 core) +Project + -> Sort + Sort Key: t1.c2 INTERNAL + Limit: 10, Offset: 20 + Send Message: [tag 1 , type MsgTopValue] + -> Table Scan on d1.t1 + Sort Key: c2 INTERNAL + Recv Message: [tag 1 , type MsgTopValue] +select * from t1 order by c2 limit 10 offset 20; +c1 c2 c3 +21 21 21 +22 22 22 +23 23 23 +24 24 24 +25 25 25 +26 26 26 +27 27 27 +28 28 28 +29 29 29 +30 30 30 drop table if exists t1; create table t1 (a int primary key, b int); insert into t1 select result, result from generate_series (1, 800000)g; diff --git a/test/distributed/cases/optimizer/top.test b/test/distributed/cases/optimizer/top.test index 4037b75d1d07..cfa67a173cf9 100644 --- a/test/distributed/cases/optimizer/top.test +++ b/test/distributed/cases/optimizer/top.test @@ -27,6 +27,9 @@ select * from t1,t2 where t1.c1=t2.c1 and t2.c2 between 33333 and 555555 order b -- @separator:table explain select * from t1,t2 where t1.c1=t2.c1 order by t1.c1 desc limit 10 offset 10; select * from t1,t2 where t1.c1=t2.c1 and t2.c2 order by t1.c1 desc limit 10 offset 10; +-- @separator:table +explain select * from t1 order by c2 limit 10 offset 20; +select * from t1 order by c2 limit 10 offset 20; drop table if exists t1; create table t1 (a int primary key, b int); insert into t1 select result, result from generate_series (1, 800000)g;