SQL Server like语句问题
我正在使用 SQL Server 2008 Enterprise。现在我有两种模式来实现相同的功能(查看zoo1或zoo2或zoo3列是否包含一些文本,在模式1中,我合并zoo1、zoo2和zoo3的内容以形成一个名为zoo的新列),我认为第一个从我的实验来看,模式总是具有更好的性能(我做了一些实验)。但我不确定我是否正确,模式1总是具有更好性能的内在原因是什么?
模式1:
Select foo, goo from tablefoo where zoo like LIKE '%'+@Query+'%'
模式2(zoo是我合并zoo1、zoo2和zoo3列的内容生成的列):
Select foo, goo from tablefoo where (zoo1 like LIKE '%'+@Query+'%') OR (zoo2 like LIKE '%'+@Query+'%') or (zoo3 like LIKE '%'+@Query+'%')
提前感谢, 乔治
I am using SQL Server 2008 Enterprise. Now I have two patterns to implement the same function (to see if zoo1 or zoo2 or zoo3 column contains some text, in pattern 1, I merge content of zoo1, zoo2 and zoo3 to form a new column called zoo), I think the first pattern is always of better performance (I have made some experiment) from my experiment. But I am not sure whether I am correct, and what is the internal reason why pattern 1 is always of better performance?
Pattern 1:
Select foo, goo from tablefoo where zoo like LIKE '%'+@Query+'%'
Pattern 2 (zoo is a column which I merge the content of column zoo1, zoo2 and zoo3 to generate):
Select foo, goo from tablefoo where (zoo1 like LIKE '%'+@Query+'%') OR (zoo2 like LIKE '%'+@Query+'%') or (zoo3 like LIKE '%'+@Query+'%')
thanks in advance,
George
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OR 几乎总是会降低性能。
在本例中,需要扫描 3 列,而不是扫描 1 列。
在这两种情况下,因为您有一个前导%,所以无论如何都不会使用索引(它可能会被扫描,因为它覆盖了zoo%列)
1列只是比3列OR查询差一些。不是“更好”。
OR almost always kills performance.
In this case, it's 3 columns to scan vs 1 column to scan.
In both cases, because you have a leading % then an index won't be used anyway (it may be scan because it's covers the zoo% columns)
The 1 column is merely less bad than the 3 column OR query. Not "better".