• drew.allen - Tuesday, November 14, 2017 8:30 AM

    gvoshol 73146 - Tuesday, November 14, 2017 8:08 AM

    drew.allen - Tuesday, November 14, 2017 7:50 AM

    gvoshol 73146 - Tuesday, November 14, 2017 5:35 AM

    Of course the absolute easiest way of doing it is
    WHERE YEAR([date]) = 2015
    But that's not sargable.  It doesn't matter if you're looking at only a few thousand rows in your table.  But if your table has hundreds of thousands or millions of rows, then performance will suffer greatly.

    I would argue that it does matter.  There is an adage that "If you fail to plan, you plan to fail."  This is very applicable to this situation, because using the SARGable method plans for growth, whereas the non-SARGable method does not.

    Drew

    I should have been more precise in my wording.
    "If you have a lower number of records, and you KNOW the number of records will not be increasing significantly, then you can use a non-Sargable function."

    You may be aware of the trade offs and willing to sacrifice performance in a given situation, but another person may not and may come along and copy your code where the performance loss is not acceptable.

    Also, it's highly unlikely that, when working with dates, the number of records will not be increasing significantly.

    Laziness is not an acceptable excuse for sacrificing performance.

    Drew

    +1000000 to that!!! If you "practice" doing it the wrong way, you'll never get good at doing it the right way.

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