做如下查询
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(索引也需要增加开销)