Query Memory??

  • Do you have any idea why the order of the conditions in the follow code would cause SqlServer's memory usage to rise from 20M to 200M ?

    declare  @UWI_Equals VARCHAR(13)

     set @uwi_equals = '0014010606000'

     

    SELECT *

    FROM [Well]

    WHERE

       ((UWI = @UWI_Equals) OR (@UWI_Equals Is Null) )  --   small memory usage: 20M

    --   ((@UWI_Equals Is Null) OR (UWI = @UWI_Equals))   -- big memory usage:  200M+

    ORDER BY UWI ASC

  • Yes,

    It "pukes" because you first compare a variable to null so it must first try to compare every record to a condition that does not exist.

    Use the first form...

    --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)

  • Snarky...=D  Thanks!

Viewing 3 posts - 1 through 2 (of 2 total)

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