Carla Wilson (9/15/2008)
Jeff, thanks for that great test script. I like your code for building 1,000,000 row table. I'm saving this code for reference.Thanks for pointing out how much more efficient LEFT is than LIKE or CHARINDEX. (I knew that, but wasn't thinking about it.)
My original WHERE clause:
WHERE Address LIKE 'XX%' would be much faster as
WHERE LEFT(Address,2) = 'XX'
It's easy to get caught up in
WHERE Col1 LIKE '%aa%'
OR Col1 LIKE '%bb%
OR Col1 LIKE 'xx%'
Maybe this is where LIKE gets the bad rap - when the code should be using LEFT() instead.
Yep... from what I can see, LIKE %aa% is faster than either %aa or aa% and LEFT/RIGHT blows that away.
Thanks for the feedback on the test table. If you have any questions on how it works, please don't hesitate to ask.
--Jeff Moden
Change is inevitable... Change for the better is not.