SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Which one of these three (2) WHERE clauses is BEST for PERFORMANCE?


Which one of these three (2) WHERE clauses is BEST for PERFORMANCE?

Author
Message
VoldemarG
VoldemarG
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 208
Which one of these [b]three (3) WHERE clauses is BEST for PERFORMANCE?[/b]

1) where databasename ='myDBname'
and charindex ('SQLAgent', applicationname)=0
and charindex ('SQL Server Log Shipping', applicationname )=0
and charindex ('Spotlight Diagnostic',applicationname )=0

2) where databasename =my'DBname'
and applicationname NOT LIKE 'SQLAgent'
and applicationname NOT LIKE ' SQL Server Log Shipping'
and applicationname NOT LIKE 'Spotlight Diagnostic'

3) where databasename ='myDBname'
and applicationname
NOT IN (''SQLAgent' ', ' SQL Server Log Shipping', 'Spotlight Diagnostic')


Voldemar
likes to play chess
ChrisM@home
ChrisM@home
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2074 Visits: 10381
Tell us - try them.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




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
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16850 Visits: 19122
I agree with Chris, you should try them.
However, you might note that No.1 is using a function so it's not SARGable and No.2 is using LIKE with no wildcards, so LIKE operator is not needed.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
VoldemarG
VoldemarG
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 208
Sorry I missed the % in LIKEs. Of course I meant wildcards..

As far as the clause with FUNCTION being NOT SARGeable - what do you mean? why not?

THANKS !!!


Voldemar
likes to play chess
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16850 Visits: 19122
This explains the basics: http://bit.ly/1aKPF3N


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88186 Visits: 41128
VoldemarG (1/30/2014)
Sorry I missed the % in LIKEs. Of course I meant wildcards..


If that's true, then the best solution (Option 3) cannot be accomplished.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
GilaMonster
GilaMonster
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89073 Visits: 45284
VoldemarG (1/30/2014)
Sorry I missed the % in LIKEs. Of course I meant wildcards..


In that case option 2 and option 3 are not equivalent and hence cannot be compared for performance.

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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


VoldemarG
VoldemarG
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 208
so from everything I heard/read, would that be fair to conclude that option 2 is the way to go in the majority of cases?

2) where databasename ='myDBname'
and applicationname NOT LIKE 'SQLAgent%'
and applicationname NOT LIKE ' SQL Server Log Shipping%'
and applicationname NOT LIKE 'Spotlight Diagnostic%'



Voldemar
likes to play chess
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26464 Visits: 17557
VoldemarG (1/31/2014)
so from everything I heard/read, would that be fair to conclude that option 2 is the way to go in the majority of cases?

2) where databasename ='myDBname'
and applicationname NOT LIKE 'SQLAgent%'
and applicationname NOT LIKE ' SQL Server Log Shipping%'
and applicationname NOT LIKE 'Spotlight Diagnostic%'


If you need the wildcard then yes that would be the best way. Option 1 is nonSARGable and option 3 is not the same thing.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16850 Visits: 19122
Sean Lange (1/31/2014)
VoldemarG (1/31/2014)
so from everything I heard/read, would that be fair to conclude that option 2 is the way to go in the majority of cases?

2) where databasename ='myDBname'
and applicationname NOT LIKE 'SQLAgent%'
and applicationname NOT LIKE ' SQL Server Log Shipping%'
and applicationname NOT LIKE 'Spotlight Diagnostic%'


If you need the wildcard then yes that would be the best way. Option 1 is nonSARGable and option 3 is not the same thing.

Note that you have a leading space on your second condition and that if you add a leading wildcard it will become nonSARGable as well. So, what's the best option? I can't give a definite answer.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search