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

  • Which one of these 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')

    Likes to play Chess

  • Tell us - try them.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • 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
  • 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 !!!

    Likes to play Chess

  • 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
  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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
  • 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%'

    Likes to play Chess

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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
  • Leading space was just my finger tapping the keyboard too intensely..

    typo..

    Likes to play Chess

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply