sql: 优化多条件查询
sql: optimize query with many conditions
我有这个 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
)
在我的服务器上,清除缓存后,return 需要两分钟多的时间才能找到大约 400 行,可能是因为我有很多 or
.
的条件
我以这种方式创建了查询,因为 Content
列中的 "number" 字符串可能在“:”和 [=] 之间具有或不具有 space 24=] 字符串或下一个数字。
有没有办法减少 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
部分
我有这个 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
)
在我的服务器上,清除缓存后,return 需要两分钟多的时间才能找到大约 400 行,可能是因为我有很多 or
.
我以这种方式创建了查询,因为 Content
列中的 "number" 字符串可能在“:”和 [=] 之间具有或不具有 space 24=] 字符串或下一个数字。
有没有办法减少 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
部分