How to clear SQL server cache / Database Query optimization task consideration


Sometime we developers assigned for query optimization tasks or performance improvment of system, and we start with it, We found some query takes very low time in the  Sql server query management studio but taking more time when calling from application, even some time we see that a query execution time varies time by time. And it seems to us like a puzzle and we go around but could not solve that problem. And it hampers our optimization task.

This happens because of some cache of SQL server, Each time a query or procedure runs, SQL server caches the query with the result, so if second time this query comes again to SQL server it returs the cache data so its take less time than before.

So to get over from this problem and to get actual execution time run the following  commands before running a query,

DBCC   FREEPROCCACHE  /* Remove all elements from the procedure cache.*/

DBCC    FREESESSIONCACHE  /*Flush the distributed query connection cache.*/

DBCC   FREESYSTEMCACHE(‘ALL’)  /* Release all unused cache entries from all caches.*/

DBCC   DROPCLEANBUFFERS  /*Remove all clean buffers from the buffer pool.*/

DBCC   PROCCACHE /*Display information about the procedure cache*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: