SQL:使用许多条件优化查询



我有这个sql查询:

SELECT
    [Id],
    [Content]    
FROM 
    [MyTable] with (nolock)
where
    Content is not null
    and (PATINDEX('%"number" : "[0-9]%', Content) > 0
         or PATINDEX('%"number":"[0-9]%', Content) > 0
         or PATINDEX('%"number" :"[0-9]%', Content) > 0
         or PATINDEX('%"number": "[0-9]%', Content) > 0
         --del
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
         or PATINDEX('%"del":"[0-9]%', Content) > 0
)

在我的服务器上,清除缓存后,返回大约 400 行需要两分钟多的时间,可能是因为我对or有很多条件。

我以这种方式创建查询是因为Content列中的"数字"字符串可能在":"和"数字"字符串或下一个数字之间有空格或不有空格。

有没有办法减少or条件?

试试吧

SELECT
    [Id],
    [Content]    
FROM 
    [MyTable] with (nolock)
where
    Content is not null
    and (PATINDEX('%"number":"[0-9]%', replace(Content,' ','')) > 0
         or PATINDEX('%"del":"[0-9]%', replace(Content,' ','')) > 0)

您也可以删除Content is not null零件

最新更新