【发布时间】:2016-04-11 12:18:22
【问题描述】:
我当前的代码中有这个 proc sql 查询。不幸的是,我正在处理超过 1000 万条记录,因此需要数小时才能运行。我一直在尝试将其转换为数据步骤,认为它会运行得更快。但是,我似乎无法获得相同的数据结果。如果有人可以帮助我完成数据步骤,我将不胜感激。或者,如果您对如何使 proc sql 更有效地运行有任何建议。
这是我的 proc sql 查询:
proc sql;
create table test as
select *
from table1 a
where exists (select 1
from table2 b
where b.acct_id = a.acct_id);
quit;
这是我尝试将其转换为的数据步骤:
proc sort data=table1; by acct_id; run;
proc sort data=table2; by acct_id; run;
data test;
merge table1 (in=a)
table2 (in=b);
by acct_id;
if a and b;
run;
【问题讨论】:
-
select * from table1 where acct_id in (select acct_id from table2)的执行速度更快吗? (该查询可能会使查询计划者的意图更清楚 - 希望节省索引/排序/连接的需要) -
至少在 SAS 9.3 中,proc sql 在优化 IN/EXIST 语句方面相当糟糕。
-
我刚刚根据你的建议做了一个小规模的测试,它似乎快得多(11 分钟对 32 秒)。我现在要试一试,看看效果如何。