Are criteria applied in order?

  • Sorry for might be a foolish question from a self-taught rookie. I have a complex SP I'm trying to improve. By necessity in the criteria there are date criteria plus string functions to break apart a text field (12345-123 representing customer-company) into its component parts so each can be tested. I'm wondering whether placing the date criteria first would prevent the string functions being applied to the entire table. The FROM clause looks like:

    WHERE SalesHeader.DOCDATE Between @FmDate And @ToDate AND

    RIGHT(RTrim(SalesHeader.CUSTNMBR), 3) = @CoCode AND

    Left(SalesHeader.CUSTNMBR, len(SalesHeader.CUSTNMBR)-4) BETWEEN @FmCust AND @ToCust

    If this is not the case, then I'll probably move the initial SELECT statements and the string functions to create numeric fields to a view, and then just query the view from the stored procedure. If it matters, there are 4 separate SELECT statements with UNION ALL to bring the data together.

    Thanks in advance for any thoughts,

    Paul

  • Nope, they're not... not without a lot of workarounds.

    You can't control the order that the WHERE clause decides to apply the filters in. You can't even control if a WHERE clause fires before or after a conversion of data in your select statement.

    The primary way to control for things like that is using temp tables to control event orders. There IS a switch to use, called OPTION ( FORCE ORDER), that combined with subqueries can give you that kind of control. I recommend you don't go near it until the answers to questions like this are second nature. It's incredibly easy to shoot yourself in the foot when 95% of the time a #table does what you need and keeps you from doing that.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Thanks Craig, I appreciate your thoughts and the speedy reply. If I'm reading correct the best performance might be:

    SELECT...

    INTO #Whatever

    WHERE DateCriteria

    then

    SELECT...

    FROM #Whatever

    WHERE StringCriteria

  • Yes that could in some cases be the most efficient means of getting the script to run properly.

    But...that is not always the case.

    You really can't determine that until you fully analyze the execution plans.

    Your date criteria might pull in 20,000 records but the @CoCode only needs to look at 200 records.

    Then if you apply those criteria together you might be down to 111 records. It may be many times more efficient to process the string related clause first and then try to narrow down the dates.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Thanks Jason. I think over time applying the date criteria first would be most efficient, but wouldn't having to apply string functions over the entire table be costly? I'm going to have to learn more about execution plans. I don't understand what I'm seeing so far.

Viewing 5 posts - 1 through 4 (of 4 total)

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