【发布时间】:2016-12-30 16:38:16
【问题描述】:
我们有一个 2 节点的 Redshift 集群,其中的表包含大约 1 亿条记录。我们将时间戳列标记为排序键——因为查询总是有时间限制的。但是,我们的用例要求结果必须按降序排序(在排序键上)。
经过一些基准测试后,我们注意到平均耗时大约 10 秒。但是,去掉逆序后,平均时间降到了 1s 以下。
是否可以将排序键的顺序反转为降序?官方文档似乎没有表明这是可能的。但是我尝试在创建新表时放置它:
sortkey(start_time DESC)
没有错误,但似乎没有任何效果。
编辑:在查询中添加了 EXPLAIN 语句的结果。
-
order_by ASC 查询
explain select * from kcdr_sr_desc where user_id=396747 and start_time > '2016-01-01' and start_time < '2016-07-01' order by start_time limit 20;结果:
XN Limit (cost=0.00..10.86 rows=20 width=300) -> XN Merge (cost=0.00..709235.56 rows=1306585 width=300) Merge Key: start_time -> XN Network (cost=0.00..709235.56 rows=1306585 width=300) Send to leader -> XN Seq Scan on kcdr_sr_desc (cost=0.00..709235.56 rows=1306585 width=300) Filter: ((user_id = 396747) AND (start_time > '2016-01-01 00:00:00'::timestamp without time zone) AND (start_time < '2016-07-01 00:00:00'::timestamp without time zone)) -
order_by DESC 查询
explain select * from kcdr_sr_desc where user_id=396747 and start_time > '2016-01-01' and start_time < '2016-07-01' order by start_time desc limit 20结果:
XN Limit (cost=1000000841967.42..1000000841967.47 rows=20 width=300) -> XN Merge (cost=1000000841967.42..1000000845233.88 rows=1306585 width=300) Merge Key: start_time -> XN Network (cost=1000000841967.42..1000000845233.88 rows=1306585 width=300) Send to leader -> XN Sort (cost=1000000841967.42..1000000845233.88 rows=1306585 width=300) Sort Key: start_time -> XN Seq Scan on kcdr_sr_desc (cost=0.00..709235.56 rows=1306585 width=300) Filter: ((user_id = 396747) AND (start_time > '2016-01-01 00:00:00'::timestamp without time zone) AND (start_time < '2016-07-01 00:00:00'::timestamp without time zone))
【问题讨论】:
-
Redshift 中没有升序或降序排序键的概念。您的基准测试结果令我惊讶。您确定这不是由代码编译引起的吗? (docs.aws.amazon.com/redshift/latest/dg/c-query-performance.html)
-
@GuiSim 我已经在两种类型的查询中添加了 EXPLAIN 的结果 - 显然,使用 order_by 作为 DESC 的查询似乎更昂贵。如果我没记错的话 - 它会在返回结果之前对整个数据范围进行排序。
标签: amazon-web-services amazon-redshift