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.

Figure 12-1<br />

Time of Creation<br />

At Execution<br />

First run/<br />

Recompile<br />

Not first run, but not in cache<br />

Not first run and in cache<br />

Added to the<br />

procedure cache in<br />

memory<br />

Chapter 12: Stored Procedures<br />

Added to:<br />

sysobjects<br />

sysdepends<br />

syscomments<br />

Optimized and compiled<br />

Recompiled<br />

After the sproc has been created, it sits in wait for the first time that it is executed. At that time, the sproc<br />

is optimized and a query plan is compiled and cached on the system. Subsequent times that we run our<br />

sproc will, unless we specify otherwise using the WITH RECOMPILE option, generally use that cached<br />

query plan rather than creating a new one. (There are situations where the sproc will be recompiled, but<br />

that is beyond the scope of this book.) This means that whenever the sproc is used it can skip much of<br />

the optimization and compilation process. Exactly how much time this saves varies depending on the<br />

complexity of the batch, the size of the tables involved in the batch, and the number of indexes on each<br />

table. Usually, the amount of time saved is seemingly small — say, perhaps 1 second or less for most<br />

scenarios — yet that difference can really add up in terms of percentage (1 second is still 100 percent<br />

faster than 2 seconds). The difference can become even more extreme when we need to make several<br />

calls or when we are in a looping situation.<br />

397

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

Saved successfully!

Ooh no, something went wrong!