日期:2014-05-18  浏览次数:20436 次

关于SQL的union效率问题,请高手来精减下这个SQL,高分送
select   iounbr   from   ln.tiou   where   lndate=20070520     and   ioust   =   '2 '   and   clttype= 'CM '     //正常
union
select   iounbr   from   ln.tiou   where   (select   coalesce(max(transdate),0)   from   ln.tlnserver   where   iounbr=ln.tiou.iounbr)=20070520     and   ioust   =   '4 '   and   clttype= 'CM '   //结清
union
select   iounbr   from   ln.tclbook   where   cldate=20070520     and   ioust   in   ( '6 ', 'A ', 'B ')   and   iounbr   in   (select   iounbr   from   ln.tiou   where   clttype= 'CM ')     //转逾期,呆滞,呆帐
union
select   iounbr   from   ln.texpbook   where   expdate=20070520     and   prmark= '1 '   and   iounbr   in   (select   iounbr   from   ln.tiou   where   clttype= 'CM ')     //转展期
union
select   iounbr   from   ln.treplnbk   where   transdate=20070520     and   capinflg= 'A '   and   trstype <> '1 '   and   iounbr   in   (select   iounbr   from   ln.tiou   where   clttype= 'CM ')//还款


上面这个SQL数据库执行要35秒,超时,有没有办法换一种写法,高效率的实现我上面的想法

------解决方案--------------------
select iounbr from ln.tiou
where
(lndate=20070520 and ioust = '2 ' and clttype= 'CM ')
or
(cldate=20070520 and ioust in ( '6 ', 'A ', 'B ') and iounbr in (select iounbr from ln.tiou where clttype= 'CM '))
------解决方案--------------------
把 in 子查询 改为关联。


------解决方案--------------------
弄张临时表行不
------解决方案--------------------
同等情况下临时表(分3次查询insert)对性能影响是否小一些?
35秒s锁,涉及表数据修改会不会收到很大影响?