【问题标题】:Redshift Query Execution PlanRedshift 查询执行计划
【发布时间】:2016-03-01 21:46:25
【问题描述】:

我注意到下面的查询运行缓慢,在详细查看后,我想知道为什么 Redshift 首先分别扫描两个表(事件和联系人),然后将它们连接在一起。联系人表中有超过 300,000 行。 我的期望是 Redshift 应该首先根据为其指定的过滤器扫描大型事件表,然后根据 Contact_IDs 列在其中查找联系人。我的期望不正确吗?我还能做些什么来加快查询速度吗?我对所有表执行了清理和分析。

查询:

select c.Segment
, Count (Distinct (CASE WHEN et.Event_ID = 1 THEN et.Contact_ID ELSE null END)) as L1
, Count (Distinct (CASE WHEN et.Event_ID = 2 THEN et.Contact_ID ELSE null END)) as L2
from
Events et 
jon contact c on c.Account_ID = et.Account_ID and c.ID = et.Contact_ID
where
et.Account_ID = 5
and et.Event_ID in (1, 2)
and et.IsGuest = 0
and et.dim_date_id >=20151125 
and et.dim_date_id <=20160226
group by c.Segment
order by 1

解释:

XN Merge (cost=1000000074927.82..1000000074927.83 rows=1 width=20)
-> XN Network (cost=1000000074927.82..1000000074927.83 rows=1 width=20)
-> XN Sort (cost=1000000074927.82..1000000074927.83 rows=1 width=20)
-> XN HashAggregate (cost=74927.80..74927.81 rows=1 width=20)
-> XN Merge Join DS_DIST_NONE (cost=0.00..74927.57 rows=31 width=20)
-> XN Seq Scan on contact c (cost=0.00..497.56 rows=39805 width=16)
-> XN Seq Scan on eventtransaction et (cost=0.00..6664.84 rows=136 width=20)

【问题讨论】:

  • @ConsiderMe 。 . . Redshift 不支持索引。它使用柱状存储。
  • 很高兴知道,谢谢

标签: sql amazon-redshift


【解决方案1】:

过滤器仅在执行连接后应用。如果您希望在应用过滤器后进行连接,我建议您创建一个临时表并将其与您在代码中指示的联系人表连接。

select c.Segment
, Count (Distinct (CASE WHEN et.Event_ID = 1 THEN et.Contact_ID ELSE null END)) as L1
, Count (Distinct (CASE WHEN et.Event_ID = 2 THEN et.Contact_ID ELSE null END)) as L2
from
(
  select Event_ID, Account_ID, Contact_ID
  FROM event
  WHERE
    et.Account_ID = 5
    and et.Event_ID in (1, 2)
    and et.IsGuest = 0
    and et.dim_date_id >=20151125 
    and et.dim_date_id <=20160226
)et 
join contact c on c.Account_ID = et.Account_ID and c.ID = et.Contact_ID
group by c.Segment
order by 1

此外,如果您在 dim_date_id 上设置了排序键,您会看到此查询的速度得到了很好的改进。更多详情请见here

【讨论】:

  • 嘿@Abiete,如果这个答案有用,你能投票赞成吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2019-10-20
相关资源
最近更新 更多