对多个列使用NOT LIKE

时间:2017-11-16 15:04:31

标签: sql sql-server

我想在多列中搜索特定值。有没有办法优化这个查询?

SELECT TOP (200) Events.EventDate, AVDCAV.CAVCH1, AVDCAV.CAVCH2, AVDCAV.CAVCH3, AVDCAV.CAVCH4, AVDCAV.CAVCH5, AVDCAV.CAVCH6, AVDCAV.CAVCH7, AVDCAV.CAVCH8, AVDCAV.CAVCH9
FROM AVDCAV
   INNER JOIN Events ON AVDCAV.IDEvent=Events.EventID
WHERE CAVCH9 NOT LIKE '0.0000000000' OR CAVCH8 NOT LIKE '0.0000000000' OR CAVCH7 NOT LIKE '0.0000000000' OR CAVCH1 NOT LIKE '0.0000000000'
ORDER BY AVDCAV.IDEvent DESC

我从SQL Express 2008 R2获取数据。

2 个答案:

答案 0 :(得分:3)

因为您没有使用任何通配符,所以您不需要LIKE

WHERE '0.0000000000' NOT IN  (CAVCH1, CAVCH7, CAVCH8, CAVCH9)

Btw表现相同,但更容易阅读。

答案 1 :(得分:0)

在不知道表格大小,当前索引等的情况下,您可以尝试添加此索引

CREATE NONCLUSTERED INDEX IX_AVDCAV ON AVDCAV ( IDEvent ) 
INCLUDE ( CAVCH1, CAVCH2, CAVCH3, CAVCH4, CAVCH5, CAVCH6, CAVCH7, CAVCH8, CAVCH9 )

查看效果是否有所改善