How to get accurate query performance? I'm attempting to improve performance of a stored procedure.  When I run the SP, it finishes almost instantly, as if something were cached.  I was told to use the following two lines of SQL before executing the SP in SSMS:

DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE


When I run the SP with the two lines of code above the SP finishes in around 8 seconds. However, is this really giving me the real execution time (as in if I run this from an application)?  How do I know?
Those commands do two things:


Clear the page cache, which stores data pages that have already been retrieved from disk (normally the biggest factor in time in a query is disk access)
Clear the query plan cache, which means the server needs to create a new query plan.  This is normally not significant except for very high transaction volumes.


You are basically getting a time that is equivalent to the "worst case" scenario - you just rebooted the server and nothing is in memory.  Subsequent runs don't need to pay the cost to pull the data from disk since those pages are already loaded into memory.

This is similar to a real world situation - your first user to run a particular query will likely have to wait longer than on subsequent runs, assuming you are checking the same data.

A good method that I like to use is running multiple times and taking an average.  This is especially helpful in a shared environment since you don't have full control over shared resources like tempdb.

You can also use these commands to get more information on what is actually happening behind the scenes:

SET STATISTICS IO ON
SET STATISTICS TIME ON


These will give you detailed information about page reads from disk (per object), logical page reads, time spent compiling a plan, and time spent executing a query.