Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Are criteria applied in order?


Are criteria applied in order?

Author
Message
pbaldy
pbaldy
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 71
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



Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5689 Visits: 7660
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
pbaldy
pbaldy
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 71
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



SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21055 Visits: 18258
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

pbaldy
pbaldy
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 71
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.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search