当使用SQL时,在WHERE子句中使用=而不是LIKE有任何好处吗?
没有任何特殊的运算符,LIKE和=是一样的,对吧?
当使用SQL时,在WHERE子句中使用=而不是LIKE有任何好处吗?
没有任何特殊的运算符,LIKE和=是一样的,对吧?
当前回答
equals(=)操作符是一个“比较操作符,比较两个值是否相等”。换句话说,在SQL语句中,除非等式两边相等,否则它不会返回true。例如:
SELECT * FROM Store WHERE Quantity = 200;
LIKE操作符“实现了模式匹配比较”,尝试将“字符串值与包含通配符的模式字符串”进行匹配。例如:
SELECT * FROM Employees WHERE Name LIKE 'Chris%';
LIKE通常只用于字符串,等号(我相信)更快。等号运算符将通配符视为文字字符。返回结果的差异如下:
SELECT * FROM Employees WHERE Name = 'Chris';
And
SELECT * FROM Employees WHERE Name LIKE 'Chris';
将返回相同的结果,尽管使用LIKE通常会花费更长的时间,因为它是一个模式匹配。然而,
SELECT * FROM Employees WHERE Name = 'Chris%';
And
SELECT * FROM Employees WHERE Name LIKE 'Chris%';
将返回不同的结果,其中使用“=”只会返回带有“Chris%”的结果,LIKE操作符将返回以“Chris”开头的任何结果。
一些好的信息可以在这里找到。
其他回答
使用=可以避免在运行时构建查询时字符串中的通配符和特殊字符冲突。
这使程序员的工作更轻松,因为不必转义所有可能滑入LIKE子句并不能产生预期结果的特殊通配符。毕竟,=是99%的用例场景,每次都必须逃避它们将是一种痛苦。
在90年代翻白眼
我也怀疑它有点慢,但如果模式中没有通配符,我怀疑它的意义。
除了通配符,=和LIKE之间的区别还取决于SQL服务器的类型和列类型。
举个例子:
CREATE TABLE testtable (
varchar_name VARCHAR(10),
char_name CHAR(10),
val INTEGER
);
INSERT INTO testtable(varchar_name, char_name, val)
VALUES ('A', 'A', 10), ('B', 'B', 20);
SELECT 'VarChar Eq Without Space', val FROM testtable WHERE varchar_name='A'
UNION ALL
SELECT 'VarChar Eq With Space', val FROM testtable WHERE varchar_name='A '
UNION ALL
SELECT 'VarChar Like Without Space', val FROM testtable WHERE varchar_name LIKE 'A'
UNION ALL
SELECT 'VarChar Like Space', val FROM testtable WHERE varchar_name LIKE 'A '
UNION ALL
SELECT 'Char Eq Without Space', val FROM testtable WHERE char_name='A'
UNION ALL
SELECT 'Char Eq With Space', val FROM testtable WHERE char_name='A '
UNION ALL
SELECT 'Char Like Without Space', val FROM testtable WHERE char_name LIKE 'A'
UNION ALL
SELECT 'Char Like With Space', val FROM testtable WHERE char_name LIKE 'A '
Using MS SQL Server 2012, the trailing spaces will be ignored in the comparison, except with LIKE when the column type is VARCHAR. Using MySQL 5.5, the trailing spaces will be ignored for =, but not for LIKE, both with CHAR and VARCHAR. Using PostgreSQL 9.1, spaces are significant with both = and LIKE using VARCHAR, but not with CHAR (see documentation). The behaviour with LIKE also differs with CHAR. Using the same data as above, using an explicit CAST on the column name also makes a difference: SELECT 'CAST none', val FROM testtable WHERE char_name LIKE 'A' UNION ALL SELECT 'CAST both', val FROM testtable WHERE CAST(char_name AS CHAR) LIKE CAST('A' AS CHAR) UNION ALL SELECT 'CAST col', val FROM testtable WHERE CAST(char_name AS CHAR) LIKE 'A' UNION ALL SELECT 'CAST value', val FROM testtable WHERE char_name LIKE CAST('A' AS CHAR) This only returns rows for "CAST both" and "CAST col".
要解决最初关于性能的问题,可以归结为索引利用率。当进行简单的表扫描时,“LIKE”和“=”是相同的。当涉及到索引时,这取决于LIKE子句是如何形成的。更具体地说,通配符的位置是什么?
考虑以下几点:
CREATE TABLE test(
txt_col varchar(10) NOT NULL
)
go
insert test (txt_col)
select CONVERT(varchar(10), row_number() over (order by (select 1))) r
from master..spt_values a, master..spt_values b
go
CREATE INDEX IX_test_data
ON test (txt_col);
go
--Turn on Show Execution Plan
set statistics io on
--A LIKE Clause with a wildcard at the beginning
DBCC DROPCLEANBUFFERS
SELECT txt_Col from test where txt_col like '%10000'
--Results in
--Table 'test'. Scan count 3, logical reads 15404, physical reads 2, read-ahead reads 15416, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Index SCAN is 85% of Query Cost
--A LIKE Clause with a wildcard in the middle
DBCC DROPCLEANBUFFERS
SELECT txt_Col from test where txt_col like '1%99'
--Results in
--Table 'test'. Scan count 1, logical reads 3023, physical reads 3, read-ahead reads 3018, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Index Seek is 100% of Query Cost for test data, but it may result in a Table Scan depending on table size/structure
--A LIKE Clause with no wildcards
DBCC DROPCLEANBUFFERS
SELECT txt_Col from test where txt_col like '10000'
--Results in
--Table 'test'. Scan count 1, logical reads 3, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Index Seek is 100% of Query Cost
GO
--an "=" clause = does Index Seek same as above
DBCC DROPCLEANBUFFERS
SELECT txt_Col from test where txt_col = '10000'
--Results in
--Table 'test'. Scan count 1, logical reads 3, physical reads 2, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
--Index Seek is 100% of Query Cost
GO
DROP TABLE test
当使用"="和"LIKE"时,在查询计划的创建中也可能存在可以忽略不计的差异。
LIKE和=是不同的。LIKE是在搜索查询中使用的。它还允许像_(简单字符通配符)和%(多字符通配符)这样的通配符。
如果你想要精确匹配,应该使用=,它会更快。
这个网站解释了LIKE
这是我对SQL 'like' vs '='性能问题的另一个答案的复制/粘贴:
一个使用mysql 5.5的个人例子:我在2个表之间有一个内部连接,一个是300万行,一个是1万行。
在索引上使用like时(没有通配符),大约需要30秒:
where login like '12345678'
使用'explain'我得到:
当对同一个查询使用'='时,大约需要0.1秒:
where login ='12345678'
使用“explain”我得到:
如您所见,类似的操作完全取消了索引seek,因此查询花费了300多倍的时间。