2016-03-03 45 views
-2

美好的一天,更高效的T-SQL語句

在我們使用SQL作爲我們的數據庫爲我們Solarwinds的產品,我已經寫了一個查詢那只是下面,我只是想知道是否有更有效的時刻執行查詢的方式。

這裏發生了什麼是我們得到不同的值被監測的設備,如果一個條件已經改變,然後提醒它看起來只是WHERE子句是矯枉過正。

所以我想要的是如果AssignmentName = x,如果currentvalue不= ='y'值然後警報。

FROM 
CustomPollerStatus INNER JOIN CustomPollerAssignmentView ON    CustomPollerStatus.CustomPollerAssignmentID = CustomPollerAssignmentView.CustomPollerAssignmentID 
LEFT OUTER JOIN CustomPollers ON CustomPollerAssignmentView.CustomPollerID =  CustomPollers.CustomPollerID 
LEFT OUTER JOIN Nodes ON CustomPollerAssignmentView.NodeID = Nodes.NodeID) 

where AssignmentName = 'a' and currentvalue != '24' 
and AssignmentName = 'b' and currentvalue != '1' 
and AssignmentName = 'c' and currentvalue != 'RUN' 
and AssignmentName = 'd' and currentvalue != 'RUN' 
and AssignmentName = 'e' and currentvalue != '72' 
and AssignmentName = 'f' and currentvalue != '30' 
and AssignmentName = 'g' and currentvalue != '72' 
and AssignmentName = 'h' and currentvalue != '30' 
and AssignmentName = 'i' and currentvalue != '276' 
and AssignmentName = 'j' and currentvalue != '72' 

我是新來的TSQL任何知識將是創造

+1

我would'nt覺得這個查詢將返回任何resluts?」 –

+0

你發佈? – Paparazzi

回答

2

我猜你更高效的查詢很大的原意是要寫

SELECT * 

FROM CustomPollerStatus 
    INNER JOIN CustomPollerAssignmentView ON CustomPollerStatus.CustomPollerAssignmentID = CustomPollerAssignmentView.CustomPollerAssignmentID 
    LEFT OUTER JOIN CustomPollers ON CustomPollerAssignmentView.CustomPollerID = CustomPollers.CustomPollerID 
    LEFT OUTER JOIN Nodes ON CustomPollerAssignmentView.NodeID = Nodes.NodeID) 

where (AssignmentName = 'a' and currentvalue != '24') 
    OR (AssignmentName = 'b' and currentvalue != '1') 
    OR (AssignmentName = 'c' and currentvalue != 'RUN') 
    OR (AssignmentName = 'd' and currentvalue != 'RUN') 
    OR (AssignmentName = 'e' and currentvalue != '72') 
    OR (AssignmentName = 'f' and currentvalue != '30') 
    OR (AssignmentName = 'g' and currentvalue != '72') 
    OR (AssignmentName = 'h' and currentvalue != '30') 
    OR (AssignmentName = 'i' and currentvalue != '276') 
    OR (AssignmentName = 'j' and currentvalue != '72') 

也許你的意思

... 
WHERE (AssignmentName IN ('a','b', 'c', 'd', 'e', 'f','g', 'h','i','j') 
    AND CurrentValue NOT IN ('24', '1','RUN','72','30''276') 

無論哪種方式查詢不會更有效率。如果你有性能問題,我會查看你的索引。

+0

的事情之前測試這是不一樣的‘一’‘1’將打破 – Paparazzi

+0

謝謝你,這是邪惡的,這些都是一些我寫的第一個SQL查詢,只是想確保我在正確的軌道,道歉我應該包括選擇語句。想看看我可以使用什麼替代方案來達到相同的答案:) – DanBran