469,299 Members | 2,093 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Using CHARINDEX vs LIKE in WHERE?

Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray
Jul 20 '05 #1
5 39932
I think they are probably nearly identical. Neither can use an index. Any
way you could narrow down the query a bit so it might be able to use an
index?
Regards,
Chuck Conover
www.TechnicalVideos.net

"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:80********************************@4ax.com...
Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray

Jul 20 '05 #2
Just to add to what Chuck said, a LIKE expression that begins with a constant
such as SELECT * FROM Table1 WHERE mycol LIKE 'value%' -can- make use of an
index, so it's probably best to get in the habit of using LIKE rather than
CHARINDEX so as to get the benefit of indexes in those cases when that does
work.

On Sat, 6 Mar 2004 15:08:37 -0700, "Chuck Conover" <cc******@commspeed.net>
wrote:
I think they are probably nearly identical. Neither can use an index. Any
way you could narrow down the query a bit so it might be able to use an
index?
Regards,
Chuck Conover
www.TechnicalVideos.net

"M Wells" <pl**********@planetthoughtful.org> wrote in message
news:80********************************@4ax.com.. .
Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray


Jul 20 '05 #3
Steve Jorgensen (no****@nospam.nospam) writes:
Just to add to what Chuck said, a LIKE expression that begins with a
constant such as SELECT * FROM Table1 WHERE mycol LIKE 'value%' -can-
make use of an index, so it's probably best to get in the habit of using
LIKE rather than CHARINDEX so as to get the benefit of indexes in those
cases when that does work.


And to add even more to what Steve and Chuck says, it is not really
true that LIKE '%val%' or charindex('value', col) cannot use an index.
What is true is that SQL Server cannot *seek* an index, that is looking
up values through the index structure. However, it can still scan the
index, and find matches this way. For a SELECT * type of query this is
not a very likely strategy, because it could lead to too many data-page
retrievals. However for the query

SELECT mycol FROM Table1 WHERE mycol LIKE '%value%'

SQL Server would use the index, since the query is covered completely
by the index.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
M Wells (pl**********@planetthoughtful.org) writes:
Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0

Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?


My vote goes for LIKE, although the support I have for this suggestion
is not solid.

Last year I ran a couple of tests to benchmark different methods to find
data using a comma-separated lists as input. There was one method using
charindex and another using LIKE. Both had absymal performance, compared
to the other method. Interestingly enough, for one of the tests LIKE
was four times faster than charindex, but only one of the three machines
on which I ran the test. On the other two machines, the timings were
very close to each other.

All this may seem very strange, until I tell you that the two machines
with identical performance were single-processor machine, whereas the
machine where LIKE was four times faster has four CPUs.

So if you are using a multi-processor, LIKE may be a better bet, if
SQL Server is better at using parallelism for LIKE. But the only way
to find is to benchmark for *your* query.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
I think a little advantage will be done on CHARINDEX search, because the
algorithm is done for a single string without joker chars while the use
of LIKE as been created for every char strings wich includes empty ones
as well as very wides ones with jokers...

A +

M Wells a écrit:
Hi All,

Just wondering if it's any more efficient to use the following SQL
statement to return a partial match from a column as opposed to the
second statement.

SELECT * FROM Table1 WHERE CHARINDEX('value', mycol) > 0
Versus:

SELECT * FROM Table1 WHERE mycol LIKE '%value%'

Does anyone have any thoughts on whether or not either of these
statements would return a result quicker than the other?

Many thanks in advance!

Much warmth,

Murray


--
Frédéric BROUARD, MVP Microsoft SQL Server. Langage SQL / Delphi / web
Livre SQL - col. Référence : http://sqlpro.developpez.com/bookSQL.html
Le site du SQL, pour débutants et pros : http://sqlpro.developpez.com
****************** mailto:br******@club-internet.fr ******************

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Samuel Walters | last post: by
11 posts views Thread by Angus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.