首页 > 解决方案 > 提高 Join in Query 的性能

问题描述

我有一个 sql(如下),我们必须将主表中的一些字段与现有的日期维度表进行比较,并过滤与purchase_date上个月最后一天相同的记录。

所以想法是将所需date的从附加date-dim到包含 的连接集的每个记录,purchase_date然后比较这些日期和过滤器。因此,我做了一个cross-join来实现这一点。

选项1:

create temporary view date_dim_sub as
select
    dt,
    fst_day_of_mth,
    lst_day_of_mth
from date_dim_tbl
where dt = add_months(${input_date}, -1);

create temporary view cust_main as
select
    c.cust_nm,
    c.cust_id,
    c.purch_date
from customer c
cross join date_dim_sub d
where c.purch_date = d.lst_day_of_mth;

但是,当我尝试运行上述 sqls 时,它需要非常长的时间来执行并且经常被挂起,迫使我们终止进程。

我曾想过为date_dim.

选项 2(使用子查询):

create temporary view cust_main as
select
    c.cust_nm,
    c.cust_id,
    c.purch_date
from customer c
where c.purch_date <> (select lst_day_of_mth from date_dim_sub where dt = add_months(${input_date}, -1));

有什么方法可以重写查询以提高性能并减轻查询挂起的任何可能性?我们正在使用 Spark-SQL。主表中有大约10M记录。

请指教。

谢谢

标签: sqlapache-spark-sqlquery-performance

解决方案


推荐阅读