Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Are criteria applied in order? Expand / Collapse
Author
Message
Posted Thursday, February 6, 2014 5:45 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, April 24, 2014 10:40 AM
Points: 35, Visits: 50
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



Post #1538957
Posted Thursday, February 6, 2014 5:49 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, December 15, 2014 2:26 PM
Points: 5,466, Visits: 7,647
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1538959
Posted Thursday, February 6, 2014 5:57 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, April 24, 2014 10:40 AM
Points: 35, Visits: 50
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



Post #1538963
Posted Thursday, February 6, 2014 6:39 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: 2 days ago @ 3:20 PM
Points: 18,064, Visits: 16,099
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
Post #1538967
Posted Friday, February 7, 2014 9:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, April 24, 2014 10:40 AM
Points: 35, Visits: 50
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.


Post #1539260
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse