日期:2014-05-17  浏览次数:20435 次

查询突然变慢,求高手看看


sql server2000 fact_pmrelate 表删除后,重新从sql 2005 fact_pmrelate导入数据800W条记录,重新建立主键(FPID,FMID,OrgNo)复合主键,及索引index_define_time(define_tie),index_define_by(define_by),跟原来表一样,但查询变慢了,现在需要三四分钟,原来只要十秒,求高手看看,不知何故

后又用DBCC reindex(fact_pmrelate,'','0')  以及更新该表统计信息,都没效果,另Import_Spread_Pmrelate 没有主键,fpid为非空。

下面语句变慢
select fpid,fmid,-8 info_type,
define_time
  from Import_Spread_Pmrelate e
 where not exists (select 1 from fact_pmrelate t where t.fpid = e.fpid) 
group by fpid,fmid,define_time

------解决方案--------------------

-- 建议:
-- 1.在fact_pmrelate表fpid字段上建索引.
create index ix_fact_pmrelate_fpid on fact_pmrelate(fpid)

-- 2.在Import_Spread_Pmrelate表fpid字段上建索引.
create index ix_Import_Spread_Pmrelate_fpid on Import_Spread_Pmrelate(fpid)

-- 3.查询语句修改为如下试试.
select e.fpid,e.fmid,-8 'info_type',e.define_time
 from Import_Spread_Pmrelate e
 left join fact_pmrelate t on e.fpid=t.fpid
 where t.fpid is null
 group by e.fpid,e.fmid,e.define_time

------解决方案--------------------
引用:
楼上的方法有效,但fact_pmrelate是大表,需要经常写入,建立索引对写的速度可能有影响。能否解释下变慢的原因,因为表的结构都一样,只是重新建立了。

1、建索引对写入的确有点影响,不过只对insert into xxx select xxxx这种形式,对于简单的INSERT into values基本上没影响。
2、如果缺少有效的索引,优化器选择扫描操作,当表越来越大,扫描所需的开销也越来越大,当达到一个临界值的时候,可能就会导致整体性能的降低。
3、除了索引,碎片问题也是个需要考虑的问题,另外NOT EXISTS不是高效的写法。