SQL运行时间太长

时间:2016-10-05 03:05:59

标签: sql

a和c都是同一个表,有400条记录。 b和d也是同一个表,有300条记录。然而,这需要大约4分钟。我怎样才能加快速度呢?

select a.docref
from a
left outer b
on a.cono=b.cono and a.tt=b.tt and a.docref=b.docref
where a.cono='VC'
and b.accn08='9005100'
and a.pstper=11609
and a.cbpref not in  (select c.cbpref
                      from  c
                      left outer join d
                      on c.cono=d.cono and c.tt=d.tt and c.docref=d.docref
                      where c.cono='VC'
                      and d.accn08='9005100'
                      and c.pstper=11609
                      and c.tt='RX')

1 个答案:

答案 0 :(得分:0)

语法NOT IN需要更多时间。尝试LEFT JOIN并添加ID IS NULL

的条件
SEELCT a.docref
FROM a
LEFT OUTER JOIN b
ON a.cono=b.cono 
AND a.tt=b.tt 
AND a.docref=b.docref
LEFT OUTER JOIN 
(
  SELECT c.cbpref
  FROM c
  LEFT OUTER JOIN d
  ON c.cono=d.cono AND c.tt=d.tt AND c.docref=d.docref
  WHERE c.cono='VC'
  AND d.accn08='9005100'
  AND c.pstper=11609
  AND c.tt='RX'
)tb
ON tb.cbpref = a.cbpref
AND a.cbpref IS NULL
WHERE a.cono='VC'
AND b.accn08='9005100'
AND a.pstper=11609