sql查询时使用临时表的速度问题

来源:互联网 发布:蜂窝数据无法选择app 编辑:程序博客网 时间:2024/05/17 08:40

做如下查询


1)不使用临时表:

select * from 库存 where 物料编号  in (select 物料编号  from 物料  where 制造厂商='abc');

500条记录耗时1s

2)使用临时表

create table #pro(procode nvarchar(30))

insert into #pro select 物料编号  from 物料  where 制造厂商='abc'

select * from 库存 where 物料编号  in (select procode from #pro);

500条记录耗时30s


原因:在库存表里 物料编号 是索引过的,而#pro 里的procode 没有索引,这样由于结构不同造成查询耗时增加。


解决:给#pro 加上索引:

create index IX_pro ON #pro(procode)

结果耗时变成3s(索引也需要增加开销)

0 0