太不可思议了:内存表竟然比永久表慢12倍以上???SQL Server怎样优化的?
看看下面两个SQL语句,注意看最后的where条件
语句A:
select a.StudNo,b.TrainPlanNo,a.AYearTermNo,b.CurSortNo,b.CurPrpNo,Credit
from StudScore a,TPlan_SpectTrainPlanCur b,ClassStudTab c,Class d,TPlan_CurBasicInfo e
where a.StudNo=c.StudNo and a.CurNo=b.CurNo and b.TrainPlanNo=d.TrainPlanNo
and b.SpectDireNo=c.SpectDireNo and c.ClassNo=d.ClassNo and a.CurNo=e.CurNo
and a.ScoringModeNo=1 and floor(a.ExamScore)>=60 and a.AYearTermNo= 20001
and b.CurSortNO=3 and b.CurPrpNo=1 --注意这里,最后的两个条件来自同一个表
语句B:
drop table aaa --使用表aaa暂存
select *
into aaa
from
(select a.StudNo,b.TrainPlanNo,a.AYearTermNo,b.CurSortNo,b.CurPrpNo,Credit
from StudScore a,TPlan_SpectTrainPlanCur b,ClassStudTab c,Class d,TPlan_CurBasicInfo e
where a.StudNo=c.StudNo and a.CurNo=b.CurNo and b.TrainPlanNo=d.TrainPlanNo
and b.SpectDireNo=c.SpectDireNo and c.ClassNo=d.ClassNo and a.CurNo=e.CurNo
and a.ScoringModeNo=1 and floor(a.ExamScore)>=60 and a.AYearTermNo= 20001
and b.CurSortNO=3 ) z --先不使用b.CurPrp=1这个条件
--下面再从表aaa中使用b.CurPrp=1这个条件
select * from aaa
where CurPrpNo=1
语句A执行要12秒,语句B不到1秒!测试数据不是很多,最后检索数据有1035条。
究竟是为什么?