1

I need some help here, I am fetching data dependent upon the conditions

Here I am using like operators, where client may use search with any word thus I have the below logic but here retrieval time is too high (it's taking 4mins) to get the data which is too slow.

I did all indexing to table but still can't optimize the query.

select * from authors where (address like '% Walmart %' OR address like 'Walmart %' OR address like '% Walmart' OR address like '% Walmart.com %' OR address like 'Walmart.com %' OR address like '% Walmart.com' OR address like '% Walmarts %' OR address like 'Walmarts %' OR address like '% Walmarts' OR address like '% Walmarts.com %' OR address like 'Walmarts.com %' OR address like '% Walmarts.com')

-- Result is 1 Rows

if i follow this logic

select * from authors where address like '%Walmart%'

-- Result is 44 rows - it is fetching all rows

But I need to get only that one row

4

6 回答 6

3

我相信这是您在条款中唯一需要的东西where,它应该涵盖包含该单词的所有内容:

select * from authors
where address like '%' + @word + '%'

您实际上已经在 where 子句的第二行执行此操作:

or like '%'+@word+'%'

这涵盖了一切,使所有其他的过度。删除这些可能会加快一点速度:

declare @title varchar(20)

set @title = 'coll' set @title = LTRIM(rtrim(@title))

select * from authors
where address like '%'+@title+'%'
于 2012-10-25T19:19:16.233 回答
2

索引很可能无济于事。在许多情况下,当您使用 Like 语句时,无法使用索引。您允许的搜索类型非常低效。这个查询很可能会扫描地址表。如您所见,如果表中有大量行,这将需要很长时间。

您可能会更好地研究全文搜索

于 2012-10-25T19:15:06.437 回答
2

如果您想在文本列上获得适当的性能,您将需要使用全文搜索。

http://msdn.microsoft.com/en-us/library/ms142571.aspx

于 2012-10-25T19:24:11.653 回答
0

索引不会帮助像这样的通配符搜索,请尝试查看全文搜索

这将利用一个索引,它可能会做一个搜索

address like @wordcom+' %

这不会使用索引,它必须扫描整个索引

address like '% '+@wordcom+' %'

如上所述,请改用全文搜索

于 2012-10-25T19:19:09.957 回答
0

正如其他人所说,您所需要的只是

select * from authors where address like '%' + @word + '%'

如果优化器知道我们告诉您的内容,这可能不会加快速度。(索引也无济于事,其他人建议使用全文搜索)。

如果您不明白为什么上面的查询就是您所需要的,让我们看看您的查询:

select * from authors
where address like @word or address like '% '+ @word
 or address like @word+' %' or address like '%'+@word+'%'
 or address like '% '+@word+' %' or address like @words
 or address like '% '+@words or address like @words+' %'
 or address like '%'+@words+'%' or address like '% '+@words+' %'
 or address like @wordcom or address like '% '+@wordcom
 or address like @wordcom+' %' or address like '%'+@wordcom+'%'
 or address like '% '+@wordcom+' %' or address like @wordscom
 or address like '% '+@wordscom or address like @wordscom+' %'
 or address like '% '+@wordscom+' %'

你确实有一个“like @word + '%'”,所以请注意,如果一条记录匹配“like @word”,它也会匹配“like @word + '%'”。所以@word 不会再添加任何结果。由于“或”,无论@word 试图匹配的结果是相同的,还是你只是得到来自@word+'%' 的结果。

应用该逻辑,您的查询可以简化为:

select * from authors
where address like @word+' %' or address like '%'+@word+'%'
 or address like @words+' %' or address like '%'+@words+'%'
 or address like @wordcom+' %' or address like '%'+@wordcom+'%'
 or address like @wordscom+' %' or address like '% '+@wordscom+' %'

同样,@word 不会添加 '%'+@word 尚未包含的任何内容,从而将查询简化为:

select * from authors
where address like '%'+@word+'%'
 or address like '%'+@words+'%'
 or address like '%'+@wordcom+'%'
 or address like '% '+@wordscom+' %'

此外,由 '%'+@word+'s%'、'%'+@word+'.com%' 或 '%'+@word+'s.com%' 找到的任何内容都将由 '%'+@word+'%' 找到.

于 2012-10-25T19:38:13.510 回答
0

如果您在查询中使用 LIKE,请使用双 %% 包装要过滤的值。这是最好的解决方案。如果您喜欢该解决方案,请投票。例如,在我的情况下,这与主查询连接在一起

filter_value = "'%%"+filter_value+"%%'"
于 2013-08-20T13:26:31.767 回答