当前位置: 代码迷 >> Sql Server >> SQLServer中子查询速度很快,拼在一起就很慢的有关问题
  详细解决方案

SQLServer中子查询速度很快,拼在一起就很慢的有关问题

热度:17   发布时间:2016-04-24 10:38:47.0
SQLServer中子查询速度很快,拼在一起就很慢的问题
查询语句是由多条子查询拼接而成,单独执行里面的子查询速度很快,但整个sql执行时会很慢很慢,请大神帮助
select a.dbno_bi,a.names_bi,a.qsq,a.class2,a.incity,a.cwnames,months_tag
,a.Turnover
,a.Achievement
,a.Quantity_sold
,a.Transaction_nb,last_Turnover
,last_Quantity_sold,last_Transaction_nb
,Turnover_cost_amt
,last_Turnover_cost_amt
 from 

select dbno_bi,names_bi,qsq,class2,a.incity,dbno,closeif,a.cwnames
    ,sum(nb*endprice) Turnover
    ,sum(nb*c.rk_cost_price) Turnover_cost_amt
    ,null Achievement
    ,sum(nb) Quantity_sold
    ,count(distinct b.nos) Transaction_nb
    from db_bi a
    left join CMD..cmd_xjjx_sale b on a.dbno = b.store 
and b.outdate >='20140407'
    and b.outdate <='20140413'
    and endprice <> 0
       left join bi_product d 
    on d.colthno = b.colthno 
    and left(b.color,2) = d.color 
    and case d.classname1 when 'ANTHONY VACCARELLO' then 'STELLALUNA'  when 'VIA SPIGA' then 'STELLALUNA'   when 'PB Clothing' then             'PIERRE BALMAIN' else d.classname1 end  in('STELLALUNA','PIERRE BALMAIN','WHATFOR','JKJY by STELLA')
    left join bi_rk_cost c 
    on   c.colthno = b.colthno 
    and left(b.color,2) = c.color
    group by dbno_bi,names_bi,qsq,class2,incity,dbno,closeif,cwnames

) a
left join(
 select dbno_bi,names_bi,qsq,class2,incity,a.cwnames
    ,sum(nb*endprice) last_Turnover
    ,null Achievement
    ,sum(nb) last_Quantity_sold
     ,sum(nb*c.rk_cost_price) last_Turnover_cost_amt
    ,count(distinct b.nos) last_Transaction_nb
    from db_bi a
    left join CMD..cmd_xjjx_sale b on a.dbno = b.store 
   and b.outdate >='20130408'
    and b.outdate <='20130414'
    and endprice <> 0
    left join bi_product d 
    on d.colthno = b.colthno 
    and left(b.color,2) = d.color 
    and case d.classname1 when 'ANTHONY VACCARELLO' then 'STELLALUNA'  when 'VIA SPIGA' then 'STELLALUNA'   when 'PB Clothing' then             'PIERRE BALMAIN' else d.classname1 end  in('STELLALUNA','PIERRE BALMAIN','WHATFOR','JKJY by STELLA')
    left join bi_rk_cost c 
    on   c.colthno = b.colthno 
    and left(b.color,2) = c.color
    group by dbno_bi,names_bi,qsq,class2,incity,dbno,closeif,cwnames

 )b 
 on a.dbno_bi = b.dbno_bi 
  and a.names_bi = b.names_bi
  and a.class2 = b.class2
  and a.incity = b.incity
left join
 (
 select  sum(jmo) months_tag ,dbno
from zg_sales_des z
where 
 z.outdate >='20140407'
 and z.outdate <='20140413'
group by dbno
)t
on a.dbno=t.dbno
where 
    a.class2 in('A')
  and a.incity in ('East')
   and a.names_bi not like '%已撤%'
  and a.closeif='0'
  order by Turnover desc

------解决方案--------------------
先把绿色的字右键,选择【缺少索引信息】,把那个索引建上去看看
  相关解决方案