日期:2014-05-18  浏览次数:20525 次

如何优化SQL语句,提高查询速度 急!!
ALTER PROCEDURE [dbo].[GetLEDShowData]
as
select rank () over (order by testtime desc) xh,Stationinfo.StationName,Report.Dpress,ZDayDunShuiHaoDian.Consumption1,History.* from History left join Stationinfo on History.StationName=Stationinfo.Agreement left join Report on Report.StationName=History.StationName
left join ZDayDunShuiHaoDian on ZDayDunShuiHaoDian.StationName=History.StationName
where History.ID in(select max(ID) from History group by StationName) and Report.ID in(select max(ID) FROM Report group by StationName) and ZDayDunShuiHaoDian.ID in(select max(ID) from ZDayDunShuiHaoDian group by StationName)
order by Testtime desc
这是我写的SQL存储过程,在SQL 2005中运行好几分钟才能查出数据来。如果要是在C#中调用那就更慢了。如果更改才能提高查询速度呢?急!

------解决方案--------------------
探讨
太长

说明各表数据量,有利于抓住重点

思路:
1、分步,利用临时表保存中间数据
2、语句修改,NOT EXISTS 代替你的MAX子查询