December 21, 2004 02:21 PM

IF Statements and Stored Procedure Performance

Rating: (6)
SQL Server Magazine
InstantDoc ID #44717

I have a stored procedure that executes one of two queries based on a value passed into it. Will I get better performance if I break the procedure into separate procedures? As I understand it, SQL Server determines a query execution plan based on the values specified the first time the query is run, so splitting up the stored procedure might help performance. But I'm not sure whether this behavior pertains to IF statements. Listing 1 shows an example of the current procedure, and Listing 2 shows an example of breaking the code into three separate procedures.

If SQL Server selects different query plans based on the different branches in the stored procedure, you're better off splitting the queries into separate stored procedures. Because SQL Server allows only one execution plan per stored procedure, leaving the queries in the same procedure will lead to stored procedure recompilation, which can reduce concurrency and increase CPU utilization.

For example, if you have multiple concurrent sessions executing the same stored procedure, your goal is to use the same execution plan. However, if the procedure's different branches cause SQL Server to recompile the execution plan, the system will have to synchronize between the sessions trying to get access to the compiled plan. Repeated recompilation of a stored procedure's execution plan is CPU intensive, so you'll also see increased CPU utilization.

The downside of breaking the queries into separate stored procedures is that you'll require more memory for the procedure cache because you'll need to hold extra plans in memory—in this case, three instead of one. To figure out how much memory the extra query plans will need, you can query the syscacheobjects system table in the master database. The result's sql column contains the query, and the sqlbytes column contains the size of the plan entry. The usecounts column shows whether or not you have plan reuse.

ARTICLE TOOLS

Add a Comment

great information

bulentgucuk 1/19/2007 6:36:05 AM


I found the explanation interesting but it got me wondering about other 'IF' statements. Will a stored procedure need to be recompiled any time an 'IF' statement is present in a stored procedure and the branch of the IF statement executed changes based on the IF criteria. In other words, if an IF statement is just setting a value based on another value (i.e. IF @a = 1 then set @b=1 else set @b=0), will the stored procedure still need to be recompiled whenever the value in @a is changed or does a stored procedure only need recompiled if the branches off of the IF statement executes a query?

Blaise 1/18/2007 1:16:11 PM


At times I get quite complex SELECT statements, where I feel tempted to put in a big IF statement, instead of using repeated CASE statements, eg

SELECT
col1,
CASE WHEN (col2 = @param1) THEN col3 ELSE col4 END AS 'baa',
CASE WHEN (col2 = @param1) THEN col5 ELSE col6 END AS 'boo',
CASE WHEN (col2 = @param1) THEN col7 ELSE col8 END AS 'buu',
col9
FROM table

What is the performance in such a scenario?

Also, I had recently a complex 4 times nested case statement and decided to create a new UDF for this and then call it in my procedure's SELECT statement. Is this good or bad?

borki 1/18/2007 12:35:58 PM


Good because it explains to some extent how execution plan is used and the pro's and con's of the options available.

ashmiles 3/3/2005 6:46:47 PM


You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS

eBooks

Playbook for a Virtualized Datacenter | During challenging times, optimizing the IT infrastructure becomes imperative. Many organizations are looking to extend their virtualization efforts to encompass the entire datacenter. Get a step-through of your technology options and more.

White Papers

As the advances in USB devices have made them invaluable to most business users’, they have also exposed organizations to enormous risks. Learn how to easily enforce device/port control and data encryption policies without requiring new infrastructure and additional admin overhead.

WEB SEMINARS

Is Flexible Lockdown Possible? Join Darren Mar-Elia for this free web webinar exploring the various methods you should consider to protect and control your desktops while also understanding the impact on the end user community.
View Seminar On-Demand.

eLearning Series

Windows IT Pro brings the experts direct to you to share their real-world perspective, experience, and expertise. During each event, three sessions stream in real time, allowing you to learn, to ask questions, and to get solutions.
Upcoming event: SQL Server Consolidation

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.