语句怎么优化啊?运行太慢了,还会出现死机select a.buildname,b.ddate as [采集时间],b.MeterJSWD as [供温
语句怎么优化啊?运行太慢了,还会出现死机 select a.buildname,b.ddate as [采集时间],b.MeterJSWD as [供温],b.MeterHSWD as [回温] ,b.MeterGY as [供压],b.MeterHY as [回压] ,b.MeterNJRL as [累计热量],b.MeterNLLL as [累计流量] ,b.Meterssll as [瞬时流量],b.Meterslrl as [瞬时热量] ,c.DoorNo,b.BuildNo,c.doorname,a.Totalhuanlu,b.bs from TBuild as a left join TMETERHistory2013 as b on a.BuildNo = b.BUILDNO inner join TDoor as c on b.BuildNo = a.BUILDNO and c.DoorNo = b.DOORNO where Meterstats = 0 and datediff(second,CONVERT(char(10),'2013-12-2 15:20:00',120) ,b.DDate) between CAST(((1 - 1 ) * 5 * 60 ) as nvarchar(100)) and CAST((1 * 5 * 60 ) as nvarchar(100)) and a.buildno=1211
[解决办法] 试试建个索引:
create index idx_TMETERHistory2013_cc on TMETERHistory2013(BUILDNO,DOORNO)
select a.buildname,b.ddate as [采集时间],b.MeterJSWD as [供温],b.MeterHSWD as [回温] ,b.MeterGY as [供压],b.MeterHY as [回压] ,b.MeterNJRL as [累计热量],b.MeterNLLL as [累计流量] ,b.Meterssll as [瞬时流量],b.Meterslrl as [瞬时热量] ,c.DoorNo,b.BuildNo,c.doorname,a.Totalhuanlu,b.bs from TBuild as a left join TMETERHistory2013 as b on a.BuildNo = b.BUILDNO inner join TDoor as c on b.BuildNo = a.BUILDNO and c.DoorNo = b.DOORNO where Meterstats = 0 and datediff(second,CONVERT(char(10),'2013-12-2 15:20:00',120) ,b.DDate) between CAST(((1 - 1 ) * 5 * 60 ) as nvarchar(100))
and CAST((1 * 5 * 60 ) as nvarchar(100)) and a.buildno=1211
a.BuildNo = b.BUILDNO 这个条件出现了2次。 另外最后的时间范围字段,最好改写成b.DDate between ... and ... 形式。 索引在能够过滤大部分记录的字段上新建