这两个查询在性能上有区别吗?
--= operator
SELECT COL1, COL2
FROM DBO.MYTABLE
WHERE COL1 = '1'
--like operator
SELECT COL1, COL2
FROM DBO.MYTABLE
WHERE COL1 LIKE '1'
基本上在这种情况下使用 LIKE 是错误的,但数据库引擎接受它。
最佳答案
检查 following post .
报价(以防下线):
My knee-jerk response was that the = would be faster, but I thought about it and realized that the query optimizer would actually see them as the same thing. A check of the query plans against a quickly-created tbFoo confirmed it. So that's what I told him.
Except that I realized a moment later that there was a major caveat - the query optimization depends on how the statement is parameterized. If it's purely ad hoc SQL and being compiled at run-time, then the statements are equivalent, but if there's going to be any plan re-use, either by including the statement in a stored proc or preparing it and executing via sp_executesql, the LIKE will impose a significant penalty.
This is because the optimizer doesn't know at compile time whether the parameter to the LIKE operator will contain a wild card, so it can't use the more specific optimization (including index selection) that it could with an = operator. So if you mostly pass parameters without wildcards, you will be executing a suboptimal query plan. Keep this in mind when designing your query!
关于SQL 服务器 : is there a performance overhead between '=' and 'like' operators?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8163515/