How to clear cache of latest select query that was run? (or prevent a select query from caching)

2.6k Views Asked by At

I am using MS SQL Server 2012. I am trying to see the optimization in the server so I can't just clear everything.

This link didn't work for me. (or this example)

Any suggestions how I can select the query and clear the cache afterwards?
(or prevent it from caching would be better)

EDIT:
Most desired outcome is testing the select query like it's running for the very first time.

1

There are 1 best solutions below

4
dean On

You can query sys.dm_exec_connections to get a handle to last executed query for a given connection, and then evict that plan with DBCC FREEPROCCACHE. Make sure you do this on another connection.

Alternatively, use WITH RECOMPILE or OPTION(RECOMPILE) to not cache the plan at all:

http://blogs.msdn.com/b/sqlmeditation/archive/2013/01/01/is-it-really-quot-recompile-quot-or-a-quot-private-temporary-compile-quot.aspx