Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
642 views
in Technique[技术] by (71.8m points)

sql-server - LIKE vs SQL Server上的CONTAINS(LIKE vs CONTAINS on SQL Server)

Which one of the following queries is faster (LIKE vs CONTAINS)?

(以下哪个查询更快(LIKE vs CONTAINS)?)

SELECT * FROM table WHERE Column LIKE '%test%';

or

(要么)

SELECT * FROM table WHERE Contains(Column, "test");
  ask by user667429 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The second (assuming you means CONTAINS , and actually put it in a valid query) should be faster, because it can use some form of index (in this case, a full text index).

(第二个(假设你的意思是CONTAINS ,实际上把它放在一个有效的查询中)应该更快,因为它可以使用某种形式的索引(在这种情况下,是一个全文索引)。)

Of course, this form of query is only available if the column is in a full text index.

(当然,只有列在全文索引中时,才能使用这种形式的查询。)

If it isn't, then only the first form is available.

(如果不是,那么只有第一种形式可用。)

The first query, using LIKE, will be unable to use an index, since it starts with a wildcard, so will always require a full table scan.

(使用LIKE的第一个查询将无法使用索引,因为它以通配符开头,因此始终需要全表扫描。)


The CONTAINS query should be:

(CONTAINS查询应该是:)

SELECT * FROM table WHERE CONTAINS(Column, 'test');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...