0
我編寫了這段代碼,它可以查找下來的節點並計算上下小時。此代碼有效,但我想知道其他方式或優化此代碼?計算停機時間的最佳方法是什麼?有什麼方式(交互方式),用戶可以輸入日期和時間間隔?如何優化我的查詢?
SELECT q1.nodeid, q1.VendorIcon, q1.Caption, q1.IP_Address, q1.OutageDurationInMinutes, q2.TimeUp FROM (SELECT Nodes.NodeID AS NodeID, ltrim(rtrim(Nodes.Caption)) Caption, Nodes.VendorIcon,Nodes.IP_Address, sum(DATEDIFF(hh, StartTime.EventTime, EndTime.EventTime)) as OutageDurationInMinutes FROM Events StartTime Left join Events EndTime On EndTime.EventType = '5' and EndTime.NetObjectType = 'N' and EndTime.NetworkNode = StartTime.NetworkNode and EndTime.EventTime = ( Select min(EventTime) from Events where EventTime>StartTime.EventTime and EventType = '5' and NetObjectType = 'N' and NetworkNode = StartTime.NetworkNode ) INNER JOIN Nodes ON StartTime.NetworkNode = Nodes.NodeID WHERE Nodes.Department = '4' AND StartTime.EventType = 1 AND StartTime.NetObjectType = 'N' AND StartTime.eventtime between dateadd(M, -1, getdate()) and getdate() Group by Nodes.NodeID,Nodes.Caption, Nodes.VendorIcon,Nodes.IP_Address, Nodes.LastBoot ) q1 INNER JOIN (SELECT Nodes.NodeID AS NodeID ,ltrim(rtrim(Caption)) Caption ,VendorIcon ,Ip_Address ,DateDiff(hour,Nodes.LastBoot,GetDate()) AS HoursUp ,CONVERT(VARCHAR(40), DATEDIFF(minute, Nodes.LastBoot, GETDATE())/(24*60)) + ' days, ' + CONVERT(VARCHAR(40), DATEDIFF(minute, Nodes.LastBoot, GETDATE())%(24*60)/60) + ' hours, and ' + CONVERT(VARCHAR(40), DATEDIFF(minute, Nodes.LastBoot, GETDATE())%60) + ' minutes.' AS TimeUp FROM [Nodes] Where LastBoot between dateadd(day, -30, getdate()) and getdate()) q2 on q1.NodeID=q2.NodeID Order by Caption
你可以嘗試發佈它在[codereview.stackexchange.com](http://codereview.stackexchange.com)上。 – 2011-05-25 15:41:38