微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

优化 Athena Query 以解决 Query 耗尽资源消息

如何解决优化 Athena Query 以解决 Query 耗尽资源消息

所有,当我尝试运行下面的查询时,我收到了“在此比例因子下查询资源耗尽”错误。此查询过去曾运行过(最多 13 个月的数据),但最近在超过 1 个月的任何跨度内成功停止运行。

我假设 map_agg/array_agg 或交叉连接是罪魁祸首。我可以对查询做些什么来使其无错误运行?

select distinct month,partner_name,service_account_id,account_source_id,account_status,map_agg(products,'yes') as products,map_agg(device_product,active_device_count) as devices,apa_active_device_types,apa_active_app_device_types

from

-- --------------------------------------

(
 select distinct coalesce(cast(accprod.month as varchar),cast(apa.month as varchar)) as month,coalesce(accprod.partner_name,apa.partner_name) as partner_name,coalesce(cast(accprod.service_account_id as bigint),cast(apa.service_account_id as bigint)) as service_account_id,accprod.account_source_id,CASE WHEN accprod.account_products IS NULL THEN ARRAY['missing'] ELSE accprod.account_products END as account_products,accprod.account_status,accprod.device_product,accprod.active_device_count,apa.apa_active_device_types,apa.apa_active_app_device_types from

-- ACCPROD

( select distinct DATE(DATE_Trunc('month',date_parse(acc.snapshot_date,'%Y-%m-%d'))) as month,acc.partner_name,acc.service_account_id,acc.account_source_id,CASE WHEN prod.account_products IS NULL THEN ARRAY['missing'] ELSE prod.account_products END as account_products,acc.account_status,acc.device_product,count(distinct concat(cast(acc.service_account_id as varchar),'-',acc.device_source_id)) as active_device_count
from  (
   select* from sb_presentation_customer1.sb_customer1_account_devices
   UNION ALL
   select* from sb_presentation_customer2.sb_customer2_account_devices
   UNION ALL
   select* from sb_presentation_customer3.sb_customer3_account_devices
   UNION ALL
   select* from sb_presentation_customer4.sb_customer4_account_devices
   ) acc


-- PRODUCT JOIN
left join (

select distinct DATE(DATE_Trunc('month',array_agg(disTINCT acc.account_product_name) as account_products

from (
   select * from sb_presentation_customer1.sb_customer1_accountproduct
   UNION ALL
   select * from sb_presentation_customer2.sb_customer2_accountproduct
   UNION ALL
   select * from sb_presentation_customer3.sb_customer3_accountproduct
   UNION ALL
   select * from sb_presentation_customer4.sb_customer4_accountproduct
  ) acc



where
DATE_Trunc('month','%Y-%m-%d')) <= (Now() - interval '1' month)
AND DATE_Trunc('month','%Y-%m-%d')) > (Now() - interval '3' month)

group by acc.service_account_id,DATE(DATE_Trunc('month','%Y-%m-%d'))),acc.partner_name

)

prod

on prod.partner_name = acc.partner_name AND prod.service_account_id = acc.service_account_id AND prod.month = DATE(DATE_Trunc('month','%Y-%m-%d')))

where
acc.device_product in ('prod1','prod2','prod3','prod4','prod5','prod6','prod7')
AND acc.device_status = 'Active'
AND acc.account_status = 'Active'
AND DATE_Trunc('month',CASE WHEN prod.account_products IS NULL THEN ARRAY['missing'] ELSE prod.account_products END,acc.device_product


) accprod

-- start Audio Play Event join

  left join

(select apa.month,apa.partner_name,apa.service_account_id,map_agg(apa.device_type,apa.apa_active_device_count) as apa_active_device_types,map_agg(apa.app_device_type,apa.apa_active_device_count) as apa_active_app_device_types

from

(
select distinct DATE(DATE_Trunc('month',date_parse(apa.timestamp_date,apa.device_type,CASE WHEN apa.device_type = 'prod1' THEN apa.device_make 
WHEN apa.app_device_type = 'prod3-X2-dev1' THEN 'dev1' 
WHEN apa.app_device_type = 'prod3-X2-dev2' THEN 'dev2' 
WHEN apa.app_device_type = 'prod3-X2-dev3' THEN 'dev3' 
WHEN apa.device_type = 'UnkNown' THEN apa.device_make 
WHEN apa.app_device_type in ('brand1','brand2','brand3','brand4') THEN 'dev2' 
ELSE apa.app_device_type END as app_device_type,count(distinct physical_device_id) as apa_active_device_count

from
( 

  select * from sb_presentation_customer1.sb_customer1_ipplayback 
  UNION ALL
  select * from sb_presentation_customer2.sb_customer2_ipplayback 
  UNION ALL
  select * from sb_presentation_customer3.sb_customer3_ipplayback 
  UNION ALL
  select * from sb_presentation_customer4.sb_customer4_ipplayback 
   ) apa



where
apa.event in ('aud1PlaybackStarted','aud1MediaOpened')
AND DATE_Trunc('month','%Y-%m-%d')) > (Now() - interval '3' month)

group by 
 DATE(DATE_Trunc('month','brand4') THEN 'dev2' 
ELSE apa.app_device_type END

 ) apa

 group by apa.month,apa.service_account_id) apa

 on cast(apa.service_account_id as varchar) = cast(accprod.service_account_id as varchar) AND apa.partner_name = accprod.partner_name AND apa.month = accprod.month
)

CROSS JOIN UNnesT(account_products) AS t (products)

group by service_account_id,month,apa_active_app_device_types

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。