您的位置:程序门 -> ms-sql server -> 基础类



关于sql的union效率问题,请高手来精减下这个sql,高分送


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于sql的union效率问题,请高手来精减下这个sql,高分送[已结贴,结贴人:yubo1209]
发表于:2007-05-22 21:14:29 楼主
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秒,超时,有没有办法换一种写法,高效率的实现我上面的想法
发表于:2007-05-22 21:17:211楼 得分:0
把条件用   or   连接起来试试.
发表于:2007-05-22 21:32:422楼 得分:0
or   不对吧   我是把多条记录   组合成一个查询里   union的用法   用or怎么用啊?
发表于:2007-05-22 21:36:543楼 得分:70
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 '))
发表于:2007-05-22 21:37:564楼 得分:10
把   in   子查询   改为关联。

发表于:2007-05-22 21:42:475楼 得分:0
dawugui(潇洒老乌龟)     谢谢您的热心

我这是5个查询数据组合成一个表,后面的四个查询都是不同的表   好象你这样写的or   是同一个表,数据不对吧?
发表于:2007-05-22 21:43:516楼 得分:0
wangtiecheng(不知不为过,不学就是错!)  
把   in   子查询   改为关联。

改过了     查询速度基本没有变化
发表于:2007-05-22 21:51:237楼 得分:10
弄张临时表行不
发表于:2007-05-22 21:59:518楼 得分:0
最好是不要用到临时表
发表于:2007-05-22 22:17:569楼 得分:10
同等情况下临时表(分3次查询insert)对性能影响是否小一些?
35秒s锁,涉及表数据修改会不会收到很大影响?
发表于:2007-05-23 09:13:5110楼 得分:0
up


快速检索

最新资讯
热门点击