日期:2014-05-17  浏览次数:20535 次

将sql代码速度变快
请高手帮忙把这段代码的速度变快,万分感谢~~
select  cast((cast(((cast((count(P0.act_power_value1)+ count(P1.act_power_value1)
+ count(P2.act_power_value1)+ count(P3.act_power_value1)+ count(P4.act_power_value1)) 
as float)/(nullif(cast((count(P0.act_power_value2)+ count(P1.act_power_value2)
+ count(P2.act_power_value2)+count(P3.act_power_value2)+ count(P4.act_power_value2))
as float),0)))*100)as decimal(18,1)))as Nvarchar)+'%'
from(SELECT  LOCALTIME as LOCALTIME FROM [DB_EasyPilot].[dbo].[WCI0NGNVSV7E48G] )as TIME_STAMP
LEFT OUTER JOIN(SELECT(case when ActivePower>0 then ActivePower else null end )
as act_power_value1,ActivePower As act_power_value2, LocalTime As tstamp_value
from [DB_EasyPilot].[dbo].[WCI0NGNVSV7E48G] 
where WindSpeed >= 3.5 AND WindSpeed <25)as P0 on time_stamp.LOCALTIME =P0.tstamp_value 
LEFT OUTER JOIN(SELECT (case when ActivePower>0 then ActivePower else null end )
as act_power_value1,ActivePower As act_power_value2, LocalTime  As tstamp_value 
from [DB_EasyPilot].[dbo].[WCI0NHNVSV3E48G]
where WindSpeed >= 3.5 AND WindSpeed  <25) as P1 on time_stamp.LOCALTIME =P1.tstamp_value 
LEFT OUTER JOIN(SELECT (case when ActivePower>0 then ActivePower else null end )
as act_power_value1,ActivePower As act_power_value2, LocalTime  As tstamp_value 
from [DB_EasyPilot].[dbo].[WCI4NHNVSV3E48G]
where WindSpeed >= 3.5 AND WindSpeed  <25) as P2 on time_stamp.LOCALTIME =P2.tstamp_value 
 LEFT OUTER JOIN(SELECT (case when ActivePower>0 then ActivePower else null end )
 as act_power_value1,ActivePower As act_power_value2, LocalTime  As tstamp_value 
 from [DB_EasyPilot].[dbo].[WCI4NHNVSV7E48G]
where WindSpeed >= 3.5 AND WindSpeed  <25) as P3 on time_stamp.LOCALTIME=P3.tstamp_value 
LEFT OUTER JOIN(SELECT (case when ActivePower>0 then ActivePower else null end )
as act_power_value1,ActivePower As act_power_value2, LocalTime As tstamp_value 
from [DB_EasyPilot].[dbo].[WCI4NGNVSV3E4AG] 
where WindSpeed >= 3.5 AND WindSpeed  <25)as P4 on time_stamp.LOCALTIME =P4.tstamp_value
where LOCALTIME >='01/12/2011' 
and LOCALTIME <'02/12/2011' 

------解决方案--------------------
LEFT OUTER JOIN(SELECT (case when ActivePower>0 then ActivePower else null end )

每个子查询都由这个 应该可以合并在一起
------解决方案--------------------
(SELECT (case when ActivePower>0 then ActivePower else null end )
as act_power_value1,ActivePower As act_power_value2, LocalTime  As tstamp_value 
from [DB_EasyPilot].[dbo].[WCI0NHNVSV3E48G]
where WindSpeed >= 3.5 AND WindSpeed  <25)
这写都有重复的们为什么要连接这么多次,可以考虑放到临时表里