SQL Server where子句使用In()vs Wildcard

[英]SQL Server where clause using In() vs Wildcard


Is there any performance difference between query A and query B?

查詢A和查詢B之間是否存在性能差異?

Query A

查詢A.

SELECT * FROM SomeTable
WHERE 1 = 1 AND (SomeField LIKE '[1,m][6,e][n]%')

Query B

查詢B.

SELECT * FROM SomeTable
WHERE 1 = 1 AND (SomeField IN ('16', 'Mens'))

1 个解决方案

#1


5  

The first could be much slower. An index can't be used with LIKE unless there is a constant prefix, for example LIKE 'foo%'. The first query will therefore require a table scan. The second query however could use an index on SomeField if one is available.

第一個可能要慢得多。除非存在常量前綴,否則索引不能與LIKE一起使用,例如LIKE'foo%'。因此,第一個查詢將需要表掃描。但是,第二個查詢可以在SomeField上使用索引(如果有的話)。

The first query will also give the wrong results as it matches '1en'.

第一個查詢也會在匹配'1en'時給出錯誤的結果。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/03/05/fc284b3f599c882b42aeb77ba0b01260.html



 
  © 2014-2022 ITdaan.com 联系我们: