SQL 服务器 : is there a performance overhead between '=' and 'like' operators?

标签 sql sql-server sql-server-2008 tsql

这两个查询在性能上有区别吗?

--= 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/

相关文章:

c# - 我如何跟踪对数据库表的任何更改

c# - 将数据表从 C# 传递到 SQL Server 2008

mysql - 使用 count(*) 和 inner join 时查询速度慢

html - 如何在 Sql Server 2008 全文搜索中忽略 html 标签

sql - 返回所有月份而不仅仅是 4

sql语句查询最后一条数据

sql-server - 在ADO Delphi XE中替换batchMove

由于权限问题,SQL Server 无法在数据库中创建表

php - 哪种方法更好地将相关数据存储在表中

MySQL INSERT SELECT 在大型静态​​表上