sql - 提高 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
记录。
请指教。
谢谢
解决方案
推荐阅读
- powerbi - 如何在 Power BI 中使用已知结束日期将项目小时数除以项目长度
- node.js - 访问公牛队列以查看来自 nodejs 的作业统计信息
- ios - 使结构可编码/可解码而不仅仅是可编码,是否有任何性能优势?
- delphi - 在某个列中的记录发生变化的 dbgrid 中减去多个值
- forms - 不可见的 reCAPTCHA javascript
- java - 在 Jenkins 文件夹上进行自动化测试以供下载时,未创建
- python - 获取未知的未知选项“-alpha”错误
- c# - 查找 GPIB 总线上的设备列表
- c# - 如何修复 .AddServices() 错误 - 扩展)IServiceCollection IServiceCollection.AddServices(IConfiguration 配置
- python - 如何使用“with open”比较python中两个文件的不同之处