Abs
Abs

Reputation: 57916

How to let SQL Server know not to use Cache in Queries?

Just a general question:

Is there a query/command I can pass to SQL Server not to use cache when executing a particularly query?

I am looking for a query/command that I can set rather than a configuration setting. Is there no need to do this?

Upvotes: 37

Views: 48411

Answers (4)

Pure.Krome
Pure.Krome

Reputation: 86957

Another more localized way to not use the MS-SQL Server Cache is to use the OPTION(RECOMPILE) keyword at the end of your statement.

E.g.

SELECT Columnname
FROM TableName
OPTION(RECOMPILE)

For more information about this and other similar query-cache clues to help identify problems with a query, Pinal Dave (no affiliation) has some helpful info about this.

Upvotes: 14

Philip Kelley
Philip Kelley

Reputation: 40309

DBCC FREEPROCCACHE

Will remove all cached procedures execution plans. This would cause all subsequent procedure calls to be recompiled.

Adding WITH RECOMPILE to a procedure definition would cause the procedure to be recompiled every time it was called.

I do not believe that (in SQL 2005 or earlier) there is any way to clear the procedrue cache of a single procedures execution plan, and I'd doubt you could do it in 2008 either.

Upvotes: 29

RickNZ
RickNZ

Reputation: 18654

If you want to force a query to not use the data cache, the best approach is to clear the cache before you run the query:

CHECKPOINT
DBCC DROPCLEANBUFFERS

Note that forcing a recompile will have no effect on the query's use of the data cache.

One reason you can't just mark an individual query to avoid the cache is the cache use is an integral part of executing the query. If the data is already in cache, then what would SQL Server do with the data if it was read a second time? Not to mention sync issues, etc.

Upvotes: 30

Paul Creasey
Paul Creasey

Reputation: 28824

use

WITH RECOMPILE

Upvotes: 4

Related Questions