June 23, 2011 at 9:43 am
Hi all,
I'm a SQL newbie who sort of inherited a 2005 db. I've got a query that returns a SUBSTRING of a field value in one column. I need to run this query today to only pull records that have a specific substring in that column.
The column containing the values is assigned an alias in the SELECT statement, and I've now learned the SELECT statement isn't processed until after the WHERE statement (yes, that's how new I am!!), so [Column Alias] = 'NCB' won't work in the WHERE statement.
So, which is more efficient?
(1)... WHERE SUBSTRING(fldField,1,3) = 'NCB'
or
(2)... WHERE fldField LIKE 'NCB%'
...or maybe something else I haven't thought of?
I know having the wildcard at the beginning of a LIKE pattern is slower, but since that's not the case here, I have no clue which of the two options above are faster for the SQL engine.
Thanks so much!
[font="Courier New"]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ SQL newbie hoping to learn ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[/font]
June 23, 2011 at 9:51 am
June 23, 2011 at 9:58 am
As you are looking from the start of the column, you can use this
WHERE LEFT(fldField,3) = 'NCB'
- just a little tidier and avoids wildcards.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
June 23, 2011 at 10:03 am
Thanks for the quick response. I suspected there might not be much difference. This db is minimally indexed because of the way it is completely repopulated twice an hour (not my design, so I'm not fully familiar with the logic).
[font="Courier New"]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ SQL newbie hoping to learn ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[/font]
June 23, 2011 at 10:04 am
Phil, you were posting as I was typing my previous post. Thanks! Tidier, indeed.
[font="Courier New"]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ SQL newbie hoping to learn ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[/font]
June 23, 2011 at 11:10 am
There's a massive difference between the two.
The first there's a function applied to the column. That makes the predicate non-SARGable, meaning SQL can NEVER use an index seek operation to satisfy that.
The second is SARGable meaning that SQL can (not necessarily will) use an index seek should there be a suitable index to seek on.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 23, 2011 at 11:21 am
GilaMonster (6/23/2011)
There's a massive difference between the two.The first there's a function applied to the column. That makes the predicate non-SARGable, meaning SQL can NEVER use an index seek operation to satisfy that.
The second is SARGable meaning that SQL can (not necessarily will) use an index seek should there be a suitable index to seek on.
This suggests that LEFT should never be used because LIKE is always either at least as fast, or faster, in terms of performance. Is that correct?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
June 23, 2011 at 1:39 pm
Correct.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 24, 2011 at 10:47 am
As someone already mentioned if the column is an indexed spetially if it is cluster index then the LIKE keyword will work faster coz it will directly search from that perticular index as you are searching for
LIKE 'char%'
but if u are searching for like'%char' then clustered index will not help for faster result.
How ever you can check for execution time for both & check which one working faster. That one is good practice.
October 14, 2015 at 6:01 pm
If you're interested in updated speed comparisons on how fast each is (well, on SQL 2014 anyway), this ➡ LIKE vs SUBSTRING vs LEFT/RIGHT vs CHARINDEX article with benchmarks makes for a great read.
October 15, 2015 at 3:16 am
_watching (10/14/2015)
If you're interested in updated speed comparisons on how fast each is (well, on SQL 2014 anyway), this ➡ LIKE vs SUBSTRING vs LEFT/RIGHT vs CHARINDEX article with benchmarks makes for a great read.
ChrisM@Work (10/7/2015)
It's quite a comprehensive test. A note about SARGable LIKE queries within the article would improve it - or a link to the same statement elsewhere in the blog. If you're Dave Lozinski, then your blog post suggestion that temp tables always get written to disk (and table variables don't) requires an update[/url].
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 15, 2015 at 6:47 pm
ChrisM@Work (10/15/2015)
_watching (10/14/2015)
If you're interested in updated speed comparisons on how fast each is (well, on SQL 2014 anyway), this ➡ LIKE vs SUBSTRING vs LEFT/RIGHT vs CHARINDEX article with benchmarks makes for a great read.ChrisM@Work (10/7/2015)
It's quite a comprehensive test. A note about SARGable LIKE queries within the article would improve it - or a link to the same statement elsewhere in the blog. If you're Dave Lozinski, then your blog post suggestion that temp tables always get written to disk (and table variables don't) requires an update[/url].
Chris - I'm not sure Dave Lozinski knows what SARGable means, since not one of queries used in the test is. No disrespect intended, but for all of the setup being done (and thumbs up on putting together a nicely formatted testbed) - every one one of these queries turns back to a table scan, since it's performing a "like '%a' OR like 'a%'" search.
With no help whatsoever from indexing, it's interesting to see that using the function is faster than not using it, but otherwise I am not sure what else it proves. With indexing though - LIKE wins fairly handily.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
October 15, 2015 at 7:10 pm
To be honest, I trust no one that uses pure RBAR to create test data nor do I trust fixed width/grooved test data where the data is usually variable in length in real life. It's almost always an indication that something else will be missed or done incorrectly that will end up in the results being skewed.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 15, 2015 at 11:29 pm
Matt Miller (#4) (10/15/2015)
ChrisM@Work (10/15/2015)
_watching (10/14/2015)
If you're interested in updated speed comparisons on how fast each is (well, on SQL 2014 anyway), this ➡ LIKE vs SUBSTRING vs LEFT/RIGHT vs CHARINDEX article with benchmarks makes for a great read.
ChrisM@Work (10/7/2015)
With no help whatsoever from indexing, it's interesting to see that using the function is faster than not using it, but otherwise I am not sure what else it proves. With indexing though - LIKE wins fairly handily.
Hmmm... not why you're saying LIKE wins fairly handily with indexing where according to the page, the table with the first results on a clustered index column, LIKE is more than 4x slower.
??
October 16, 2015 at 12:25 am
_watching (10/15/2015)
Matt Miller (#4) (10/15/2015)
ChrisM@Work (10/15/2015)
_watching (10/14/2015)
If you're interested in updated speed comparisons on how fast each is (well, on SQL 2014 anyway), this ➡ LIKE vs SUBSTRING vs LEFT/RIGHT vs CHARINDEX article with benchmarks makes for a great read.ChrisM@Work (10/7/2015)
With no help whatsoever from indexing, it's interesting to see that using the function is faster than not using it, but otherwise I am not sure what else it proves. With indexing though - LIKE wins fairly handily.Hmmm... not why you're saying LIKE wins fairly handily with indexing where according to the page, the table with the first results on a clustered index column, LIKE is more than 4x slower.
??
Did you read Matt Miller's comment a few posts above? Try making the query SARGable, re test and post the results.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
Viewing 15 posts - 1 through 15 (of 26 total)
You must be logged in to reply to this topic. Login to reply