30.06.2013 Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

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 3: Query Plan Metadata<br />

--Determine the id of your database<br />

DECLARE @intDBID INTEGER<br />

SET @intDBID = ( SELECT dbid<br />

FROM master.dbo.sysdatabases<br />

WHERE name = 'mydatabasename'<br />

)<br />

--Flush the procedure cache for your database<br />

DBCC FLUSHPROCINDB (@intDBID)<br />

Listing 3.1: Flushing the cache of plans belonging to a particular database.<br />

The flushed plans will go back into the cache the next time they are executed, the result<br />

being that most of the stored procedures that are run frequently and are part of your<br />

normal workload will have a similar cache time. This will make the statistics much easier<br />

to interpret (until and unless they get recompiled again for some other reason).<br />

Flushing the cache is a controversial suggestion; many DBAs avoid doing it on a production<br />

server. After all, clearing the cache means that new plans need to be determined and<br />

created, which has an associated cost. It's true that recompiling all of the query plans will<br />

cause some extra work for your processor(s), but many modern processors shrug this off<br />

<strong>with</strong> little effect beyond a brief (few seconds) spike of CPU activity.<br />

Viewing the Text of Cached Queries<br />

and Query Plans<br />

The root cause of many performance problems is the fact that the plan you get when<br />

query execution is optimized under a full production server load can be very different<br />

from the one you saw in <strong>Management</strong> Studio while building the query. For example, on<br />

your development server, you may have only a single CPU, several orders of magnitude<br />

fewer rows, different amounts of RAM, statistics that are completely up to date, fewer<br />

concurrent users, and so on.<br />

78

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

Saved successfully!

Ooh no, something went wrong!