Tsql 更高效的T-SQL语句

Tsql 更高效的T-SQL语句,tsql,Tsql,你好 目前,我们使用SQL作为Solarwinds产品的数据库,我在下面写了一个查询,我只是想知道是否有更有效的方法来执行查询 这里发生的事情是,我们为被监视的设备获取不同的值,如果某个条件发生了变化,那么就发出警报,似乎WHERE子句是多余的 所以我想要的是如果AssignmentName=x,如果currentvalue不等于'y'值,则发出警报 FROM CustomPollerStatus INNER JOIN CustomPollerAssignmentView ON

你好

目前,我们使用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新手,任何知识都有助于创建更高效的查询

我猜您是想编写

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')

无论哪种方式,查询都不会更有效率。如果您有性能问题,我会查看您的索引。

我想这个查询不会返回任何reslut?“您在发布之前测试过这个吗?否则“a”“1”就不一样了谢谢,这太糟糕了,这是我写的第一批SQL查询,只是想确保我的思路正确,很抱歉我应该包含Select语句。”。想看看我能用什么替代方案得出相同的答案:)