我在使用' LEFT JOIN LATERAL'使用postgresql 9.5。
在我的表格中,有三列' ID',' DATE',' CODE'。一个人(ID)有多行,如下所示。 ID的数量是362,总行数约为2500000。
ID / DATE / CODE
1 / 20020101 / drugA
1 / 20020102 / drugA
1 / 20020103 / drugB
1 / 20020104 / drugA
1 / 20020105 / drugA
1 / 20020106 / drugB
1 / 20020107 / drugA
2 / ... / ...
我需要总结在药物B的第一天和最后一天之间使用的药物A的信息。
在上述情况下,ID(1)只能保留两行[20020103~20020106;药物治疗期间。]
1 / 20020104 / drugA
1 / 20020105 / drugA
为了完成这项工作,我使用' LEFT LATERAL JOIN'编写SQL代码。如下。
SELECT * FROM (SELECT ID, min(DATE) as start_date, max(DATE) as end_date from MAIN_TABLE WHERE CODE = 'drugA' GROUP BY ID) AA
LEFT JOIN LATERAL (SELECT ID, COUNT(ID) as no_tx, min(DATE) as fday_tx, max(DATE) lday_tx from MAIN_TABLE WHERE CODE = 'drugB' AND DATE > AA.start_date AND DATE < AA.end_date GROUP BY ID) as BB USING(ID);
只有362人ID,但这个postgresql代码大约需要2分钟。
太慢了。因此,我使用子查询尝试了另一个SQL代码。
SELECT * FROM (SELECT ID, min(DATE) as start_date, max(DATE) as end_date from MAIN_TABLE WHERE CODE ='drugA' GROUP BY ID) AA
LEFT JOIN (
SELECT ID, COUNT(ID) as no_tx, min(DATE) as fday_tx, max(DATE) lday_tx FROM (SELECT ID, DATE, CODE FROM MAIN_TABLE) BB
LEFT JOIN (SELECT ID, min(DATE) as start_date, max(DATE) as end_date from MAIN_TABLE WHERE CODE ='drugA' GROUP BY ID) CC USING (ID)
WHERE CODE = 'drugB' and DATE > start_date and DATE < end_date GROUP BY ID
) DD USING (ID);
这段代码并不简单,但速度非常快(仅需1.6秒)。
当我比较两个代码的解释时,第二个代码使用散列连接,但第一个代码没有。
我是否可以通过“LEFT LATERAL JOIN&#39;来改进第一个代码?功能更有效?
答案 0 :(得分:2)
为什么不使用join
和group by
?
SELECT AA.ID, COUNT(B.ID) as no_tx, min(B.DATE) as fday_tx, max(B.DATE) as lday_tx,
AA.start_date, AA.end_date
FROM (SELECT ID, min(DATE) as start_date, max(DATE) as end_date
FROM MAIN_TABLE
WHERE CODE = 'drugA'
GROUP BY ID
) AA LEFT JOIN
MAIN_TABLE b
ON b.CODE = 'drugB' AND b.DATE > AA.start_date AND b.DATE < AA.end_date
GROUP BY AA.ID, AA.start_date, AA.end_date;
或者,更有效的是,窗口函数:
SELECT ID, SUM(CASE WHEN code = 'drugB' THEN 1 ELSE 0 END) as no_tx,
MIN(CASE WHEN code = 'drugB' THEN DATE END) as fday_tx,
MIN(CASE WHEN code = 'drugB' THEN DATE END) as lday_tx,
start_date, end_date
FROM (SELECT t.*,
MIN(CASE WHEN code = 'drugA' THEN date END) as start_date,
MAX(CASE WHEN code = 'drugB' THEN date END) as end_date
FROM MAIN_TABLE t
) t
WHERE code in ('drugA', 'drugB') AND
date between start_date and end_date
GROUP BY t.id;