Is DBCC DROPCLEANBUFFERS transaction safe? - sql-server

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

Related

Why is my Sql Query is Faster the Second Time it Runs?

Every time I am getting same execution time while executing the SQL query.
is there any chance to get same execution time for all the time if SQL query runs multiple times?
When you run your query for the first time and the data is not in cache, the server read the data from disk. It is time-comsuming. The second time you execute the same query data is already in cache so it requires less time.
is there any chance to get same execution time for all the time if SQL
query runs multiple times ?
If you want to test your queries with the cold cache (with no data cached, every time) you can use DBCC DROPCLEANBUFFERS before your query execution:
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 and columnstore objects from the columnstore
object 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.
Of course this is not to be used in production environment.
If
conversely you want to have your data always in cache, you should increase RAM for your server and don't restart it as long as possible.

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 Server : same query running multiple times with different execution time

I have a very simple query
SELECT count(*) FROM MyTable
There are 508,000 rows in it. When I run the above query for the very first time it takes approximately 53 seconds to return the result. If I rerun the query again, it takes milliseconds to return the result.
I believe SQL Server is caching the query/results? Is it possible that I could tell SQL Server not to cache query/results, or somehow clear the cache/result?
In my application I am trying to do some performance tuning, but problems like above don't help me out.
Yes, you can flush the data buffer like this (but seriously, don't!):
DBCC DROPCLEANBUFFERS
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.
Ref
You have already ruled out the database as your bottleneck (which it is often), so rather than freeing the data buffer cache, I suggest you profile your code.

SQL - clearing execution cache

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

Resources