Is there any performance difference between the following?
NAME LIKE '%EXPRESSION_1%'
OR NAME LIKE '%EXPRESSION_2%'
...
OR NAME LIKE '%EXPRESSION_N%'
VS
NAME IN (ACTUAL_VALUE_1,ACTUAL_VALUE_2,.., ACTUAL_VALUE_N)
Is there any performance difference between the following?
NAME LIKE '%EXPRESSION_1%'
OR NAME LIKE '%EXPRESSION_2%'
...
OR NAME LIKE '%EXPRESSION_N%'
VS
NAME IN (ACTUAL_VALUE_1,ACTUAL_VALUE_2,.., ACTUAL_VALUE_N)
The IN
version is potentially much, much faster.
The two versions do not do the same thing. But, if either meets your needs, the IN
version can take advantage of an index on NAME
. The LIKE
version cannot, because the pattern starts with a wildcard.
You could write this as:
WHERE NAME LIKE 'EXPRESSION_%'
If this meets your needs, it can also take advantage of an index on NAME
.
You can simply try to use
NAME LIKE '%EXPRESSION_%'
As far as the performance is concerned then IN is comparatively faster than OR. Also you can confirm the performance using the execution plan of your two queries.
Also as commented above, the two queries which you are showing are different.
The first query:
NAME LIKE '%EXPRESSION_1%'
OR NAME LIKE '%EXPRESSION_2%'
...
OR NAME LIKE '%EXPRESSION_N%'
will try to fetch the result which has sample data like
EXPRESSION_123
XXXEXPRESSION_1234
EXPRESSION_2323
EXPRESSION_2......
whereas your second query will match the records which are exactly matching to
ACTUAL_VALUE_1,ACTUAL_VALUE_2.....
If you are using variable expression which can be change according to the given parameter. Then use of
declare @Expression1 varchar(50)
Set @Expression2 = '%'+ @Expression1 +'%'
NAME LIKE @Expression2
so whatever parameter will come in @Expression1 it will automatically take care of it.