In the most cases msdn is a trusted source of information. Of course sometimes some mistakes are slipping into. The other day I read a documentation about CREATING STORED PROCEDURES when I found an interesting slip-up:
“Avoid using a wildcard as the leading character in a LIKE clause, for example, LIKE ‘%a%’. Because the first character is non-deterministic, the query processor is unable to use available indexes. Use LIKE ‘a%’ instead.”
Well, I thought there was a catch in it because I use the LIKE operator with leading wildcard in many cases and I know, we can get a significant speed increase with indexes in this case too.
I made a simple test using my Northwind database:
-- the using of indexex with leading wildcards
create procedure SelectCustomers
as begin
select * from Customers
where CompanyName like 'a%'
end
go
-- running the example
exec SelectCustomers
go
-- delete the example
drop procedure SelectCustomers
After running I got the following result in my actual execution plan:
The conclusion about using indexes with LIKE operator is the following:
- without wildcards: Index seek (like ‘a’)
- with wildcards: Index scan (like ‘a%’, like ‘%a’, like ‘%a%’)
Very informative article. Its really helped me lot. Thanks for sharing with us. Check out this link too its also having a nice post related to this post with wonderful explanation...
ReplyDeletehttp://mindstick.com/Articles/1216a153-610b-41f1-b51b-3035c069ffd7/?%E2%80%98SELECT%E2%80%99%20command%20with%20SQL%20operator
Thanks
I have read this. What should it explain to me about indexes and like operator?
ReplyDeleteAnyway, stop programming and back to the family. Christmas is comming. :))
Morzel