Use Condition-Specific Stored Procedures
The optimizer chooses the best plan for an SP given the current parameter values. This plan is then reused regardless of whether the nature of the parameters changes from call to call. If an application is aware that input parameters have a small number of significant behaviors, then the application may use condition-specific SPs.
For example, if an SP has a single parameter which can be either NULL or non-NULL, and a NULL value selects very few rows while a non-NULL value selects many more, then this technique may be used. The application would code the SP twice. The two SPs will have different names but the SP contents will be the same. The logic that calls the SP must call the appropriate SP depending on whether the parameter value is NULL or non-NULL. SPs are compiled the first time they are called. As a result, the SP for NULL will be optimized for a NULL value and will be subsequently only called when the parameter value is NULL. The SP for a non-NULL parameter will similarly be optimized for a non-NULL parameter value and only be called with a non-NULL parameter value. This method can result in dramatic improvements in performance when the degree of selectivity of parameters varies significantly.
Comments
Anonymous
November 10, 2005
The comment has been removedAnonymous
March 30, 2006
Thanks Ian!
What about EXEC WITH RECOMPILE? If the SP is simple but the query'e result very dependent on parameters I think RECOMILE is fine?
Maurits! Have look at this:
http://www.sommarskog.se/dyn-search.html#dynsummaryAnonymous
April 21, 2006
Your comments seem in conflict with http://blogs.msdn.com/queryoptteam/archive/2006/03/31/565991.aspxAnonymous
June 06, 2006
The comment has been removedAnonymous
June 29, 2008
PingBack from http://xzavier.finestlovestories.com/caseusageinwhereconditioninsql.htmlAnonymous
June 01, 2009
PingBack from http://paidsurveyshub.info/story.php?id=75516Anonymous
June 18, 2009
PingBack from http://barstoolsite.info/story.php?id=4467