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

帮忙一个查询语句的优化!谢谢
select   hzjxsdm,isnull(kkyh, ' ')   as   kkyh,max(hzjxsmc)   as   hzjxsmc,max(sf)   as   sf,isnull(count(*),0)   as   sl,
isnull(sum(ygje),0)   as   sumygje,isnull(sum(ysbj),0)   as   sumysbj,
isnull(sum(yslx),0)   as   sumyslx   from  
(select   a.khmc,a.hzjxsdm,a.hzjxsmc,a.sf,a.hth,a.ysbj,a.yslx,a.ygje,a.qs,
case   when   charindex( '十堰 ',kkyh)> 0   and   (charindex( '建设 ',kkyh)> 0   or   charindex( '建行 ',kkyh)> 0)then  
'中国建设银行股份有限公司十堰分行 '   when   isnull(kkyh, ' ')= ' '   then   '统一扣款 '   else   kkyh   end   kkyh,b.kkyhzh   from   ht   b   with(nolock),   ysygmx   a   with(nolock)  
  where   a.yxbz= '1 '   and   b.yxbz= '1 '   and   a.hth=b.hth   and   convert(char(6),a.hkrq,112)= '200709 '
  and   b.hzjxsdm   in(select   hzjxsdm   from   gcxxtz   with(nolock)where   cxbz   = '柳汽 '   and   yxbz= '1 ')   )   a   group   by   hzjxsdm,isnull(kkyh, ' ')  
order   by   hzjxsdm  

  这个查询语句执行时需要1分12秒。如果去掉and   b.hzjxsdm   in(select   hzjxsdm   from   gcxxtz   with(nolock)where   cxbz   = '柳汽 '   and   yxbz= '1 ')   需要1秒钟。   怎么样修改才可以加快速度!!谢谢大家
      ht   b   15W条数据
    ysygmx   a   31W条数据
gcxxtz   1000条数据!

------解决方案--------------------
and b.hzjxsdm in(select hzjxsdm from gcxxtz with(nolock)where cxbz = '柳汽 ' and yxbz= '1 ')


==>

and exists(select 1 from gcxxtz with(nolock) where cxbz = '柳汽 ' and yxbz= '1 ' and hzjxsdm=b.hzjxsdm)