SQL - clearing execution cache - sql-server

I'm trying to profile a few queries but as you know, when you run a query a second time, it comes back in 0ms. I'm using
DBCC FREEPROCCACHE
but that doesn't seem to do the trick. what else can I run to clear any trace of execution/results cache?

CHECKPOINT;
DBCC dropcleanbuffers;
This should not be run on a production server though. CHECKPOINT is a database scoped command that will write the dirty buffers to disc so they will be affected by the next command but DBCC dropcleanbuffers is global and all data pages dropped from the buffer cache in this manner will need to be read in from disc when used next time.

You'll also want to use DBCC DROPCLEANBUFFERS. This will test the queries with a cold buffer cache.
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS

Related

Is DBCC DROPCLEANBUFFERS transaction safe?

I executed DBCC DROPCLEANBUFFERS before executing CHECKPOINT only to kill the executing query in between when i realised I could have dirty pages in the memory. It is recommended to run CHECKPOINT before DROPCLEANBUFFERS to write any dirty pages to disk. Since it was a blackbox machine and I would have no idea as to what data was cached , Would I have lost any data even though I stopped the query completion?
It will not be a problem. DBCC DROPCLEANBUFFERS only drops the clean buffers from the buffer cache. Dirty pages (modified pages) will not be dropped by DBCC DROPCLEANBUFFERS.
On the other hand, CHECKPOINT writes the dirty pages(modified pages) to disk. So, it is not going to work with Clean buffers in buffer cache.
Generally, it is recommended to first issue CHECKPOINT to write dirty pages to disk, so that, there will be only clean buffers in the buffer cache.Then issue DBCC DROPCLEANBUFFERS, which drops all the buffers in the buffer cache.
You can read more on this at MSDN

Query terrible slow after DBCC DROPCLEANBUFFERS / FREEPROCCACHE

I have a complex query that runs under 2 seconds (which is ok), however if i run
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
first the query performans goes oder 40 seconds. Should I be worried?
No, Sql server is doing EXACTLY what you just told it to. You asked it to empty out ALL it's hard earned buffers.
It ran first previously because it had stored the information about the tables and the query in the caches and deleted the execution plans. Having purged them it has to rebuild them from scratch.
Don't do that unless you've a good reason to.
Use DBCC FREEPROCCACHE to clear the plan cache carefully. Freeing the plan cache causes, for example, a stored procedure to be recompiled instead of reused from the cache. This can cause a sudden, temporary decrease in query performance. For each cleared cachestore in the plan cache, the SQL Server error log will contain the following informational message: "SQL Server has encountered %d occurrence(s) of cachestore flush for the ‘%s’ cachestore (part of plan cache) due to ‘DBCC FREEPROCCACHE’ or ‘DBCC FREESYSTEMCACHE’ operations." This message is logged every five minutes as long as the cache is flushed within that time interval.
Use DBCC DROPCLEANBUFFERS to test queries with a cold buffer cache without shutting down and restarting the server.
To drop clean buffers from the buffer pool, first use CHECKPOINT to produce a cold buffer cache. This forces all dirty pages for the current database to be written to disk and cleans the buffers. After you do this, you can issue DBCC DROPCLEANBUFFERS command to remove all buffers from the buffer pool.

Clearing the cache (recompile) for a database in sqlserver without restarting it sqlserver

Is there a way to clear all of the cache for a specific database somewhat equivalent to what OPTION(RECOMPILE) does for a stored proc?
I think the closest thing is:
DBCC DROPCLEANBUFFERS
More on: MSDN
Look at remarks section closely:
Use DBCC DROPCLEANBUFFERS to test queries with a cold buffer cache
without shutting down and restarting the server. To drop clean buffers
from the buffer pool, first use CHECKPOINT to produce a cold buffer
cache. This forces all dirty pages for the current database to be
written to disk and cleans the buffers. After you do this, you can
issue DBCC DROPCLEANBUFFERS command to remove all buffers from the
buffer pool.
You will usually use this:
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
What cache are you talking about?
If Procedure Cache you can use DBCC FLUSHPROCINDB(<db_id>);
If all caches, including procedure cache and buffer cache, you can use
ALTER DATABASE YourDB SET OFFLINE --WITH ROLLBACK IMMEDIATE
ALTER DATABASE YourDB SET ONLINE

SQL Query performance test

I would like to check the performance of my SQL query but every time I execute the same query I am getting different execution time due to cache and statistics.
I tried to use the following commands before every cycle but the problem persists:
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS

How can I clear the SQL Server query cache?

I've got a simple query running against SQL Server 2005
SELECT *
FROM Table
WHERE Col = 'someval'
The first time I execute the query can take > 15 secs. Subsequent executes are back in < 1 sec.
How can I get SQL Server 2005 not to use any cached results? I've tried running
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
But this seems to have no effect on the query speed (still < 1 sec).
Here is some good explaination. check out it.
http://www.mssqltips.com/tip.asp?tip=1360
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
From the linked article:
If all of the performance testing is conducted in SQL Server the best approach may be to issue a CHECKPOINT and then issue the DBCC DROPCLEANBUFFERS command. Although the CHECKPOINT process is an automatic internal system process in SQL Server and occurs on a regular basis, it is important to issue this command to write all of the dirty pages for the current database to disk and clean the buffers. Then the DBCC DROPCLEANBUFFERS command can be executed to remove all buffers from the buffer pool.
Eight different ways to clear the plan cache
1. Remove all elements from the plan cache for the entire instance
DBCC FREEPROCCACHE;
Use this to clear the plan cache carefully. Freeing the plan cache causes, for example, a stored procedure to be recompiled instead of reused from the cache. This can cause a sudden, temporary decrease in query performance.
2. Flush the plan cache for the entire instance and suppress the regular completion message
"DBCC execution completed. If DBCC printed error messages, contact your system administrator."
DBCC FREEPROCCACHE WITH NO_INFOMSGS;
3. Flush the ad hoc and prepared plan cache for the entire instance
DBCC FREESYSTEMCACHE ('SQL Plans');
4. Flush the ad hoc and prepared plan cache for one resource pool
DBCC FREESYSTEMCACHE ('SQL Plans', 'LimitedIOPool');
5. Flush the entire plan cache for one resource pool
DBCC FREEPROCCACHE ('LimitedIOPool');
6. Remove all elements from the plan cache for one database (does not work in SQL Azure)
-- Get DBID from one database name first
DECLARE #intDBID INT;
SET #intDBID = (SELECT [dbid]
FROM master.dbo.sysdatabases
WHERE name = N'AdventureWorks2014');
DBCC FLUSHPROCINDB (#intDBID);
7. Clear plan cache for the current database
USE AdventureWorks2014;
GO
-- New in SQL Server 2016 and SQL Azure
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;
8. Remove one query plan from the cache
USE AdventureWorks2014;
GO
-- Run a stored procedure or query
EXEC dbo.uspGetEmployeeManagers 9;
-- Find the plan handle for that query
-- OPTION (RECOMPILE) keeps this query from going into the plan cache
SELECT cp.plan_handle, cp.objtype, cp.usecounts,
DB_NAME(st.dbid) AS [DatabaseName]
FROM sys.dm_exec_cached_plans AS cp CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st
WHERE OBJECT_NAME (st.objectid)
LIKE N'%uspGetEmployeeManagers%' OPTION (RECOMPILE);
-- Remove the specific query plan from the cache using the plan handle from the above query
DBCC FREEPROCCACHE (0x050011007A2CC30E204991F30200000001000000000000000000000000000000000000000000000000000000);
Source 1 2 3
Note that neither DBCC DROPCLEANBUFFERS; nor DBCC FREEPROCCACHE; is supported in SQL Azure / SQL Data Warehouse.
However, if you need to reset the plan cache in SQL Azure, you can alter one of the tables in the query (for instance, just add then remove a column), this will have the side-effect of removing the plan from the cache.
I personally do this as a way of testing query performance without having to deal with cached plans.
More details about SQL Azure Procedure Cache here
While the question is just a bit old, this might still help. I'm running into similar issues and using the option below has helped me. Not sure if this is a permanent solution, but it's fixing it for now.
OPTION (OPTIMIZE FOR UNKNOWN)
Then your query will be like this
select * from Table where Col = 'someval' OPTION (OPTIMIZE FOR UNKNOWN)
EXEC sys.sp_configure N'max server memory (MB)', N'2147483646'
GO
RECONFIGURE WITH OVERRIDE
GO
What value you specify for the server memory is not important, as long as it differs from the current one.
Btw, the thing that causes the speedup is not the query cache, but the data cache.

Resources