17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12: Stored Procedures<br />

When a Good Sproc Goes Bad<br />

Perhaps one of the most important things to recognize on the downside of sprocs is that, unless you<br />

manually interfere (using the WITH RECOMPILE option), they are optimized based on either the first time<br />

that they run or when the statistics have been updated on the table(s) involved in any queries.<br />

That “optimize once, use many times” strategy is what saves the sproc time, but it’s a double-edged<br />

sword. If our query is dynamic in nature (the query is built up as it goes using the EXEC command), then<br />

the sproc may be optimized for the way things ran the first time, only to find that things never run that<br />

way again — in short, it may be using the wrong plan!<br />

It’s not just dynamic queries in sprocs that can cause this scenario either. Imagine a web page that lets us<br />

mix and match several criteria for a search. For example, let’s say that we wanted to add a sproc to the<br />

AdventureWorks<strong>2008</strong> database that would support a web page that allows users to search for an order<br />

based on:<br />

❑ Customer name<br />

❑ Sales Order ID<br />

❑ Product ID<br />

❑ Order date<br />

The user is allowed to supply any mix of the information, with each new piece of information supplied<br />

making the search a little more restricted and theoretically faster.<br />

The approach we would probably take to this would be to have more than one query and to select the right<br />

query to run depending on what was supplied by the user. The first time that we execute our sproc, it is<br />

going to run through a few IF...ELSE statements and pick the right query to run. Unfortunately, it’s just<br />

the right query for that particular time we ran the sproc (and an unknown percentage of the other times).<br />

Any time after that first time the sproc selects a different query to run, it will still be using the query plan<br />

based on the first time the sproc ran. In short, the query performance is really going to suffer.<br />

Using the WITH RECOMPILE Option<br />

398<br />

We can choose to use the security and compartmentalization of code benefits of a sproc, but still ignore<br />

the precompiled code side of things. This lets us get around this issue of not using the right query plan,<br />

because we’re certain that a new plan was created just for this run. To do this, we make use of the WITH<br />

RECOMPILE option, which can be included in two different ways.<br />

First, we can include the WITH RECOMPILE at runtime. We simply include it with our execution script:<br />

EXEC spMySproc ‘1/1/2004’<br />

WITH RECOMPILE<br />

This tells <strong>SQL</strong> <strong>Server</strong> to throw away the existing execution plan and create a new one — but just this<br />

once. That is, just for this time that we’ve executed the sproc using the WITH RECOMPILE option.<br />

We can also choose to make things more permanent by including the WITH RECOMPILE option right<br />

within the sproc. If we do things this way, we add the WITH RECOMPILE option immediately before our<br />

AS statement in our CREATE PROC or ALTER PROC statements.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!