Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prevent repeat calls of a SQL query from caching and being quicker?

Tags:

sql

sql-server

I need to get several queries to run in ~1-2 seconds. I've been working on optimizing them, but it takes ~20 seconds the first time any of them are called and ~1 on all subsequent calls. This is making it impossible to tell if any changes I make are speeding up the query, because it always runs in ~1 second afterwards. I'm not incredibly familiar with SQL, but from what i've been able to learn it seems like something is caching. I'm trying to figure out how to prevent this, but nothing seems to work. From what i've found off Google, people have been suggesting

DBCC FREEPROCCACHE

or

OPTION(recompile)

Neither of these seem to work though. Each query is still running in ~1 second when they took ~20 the first time. I just want to make sure the changes i'm making are causing improvements, and not that the improvements are coming from caching. Is there some other trick to do this?

like image 588
user1652427 Avatar asked Nov 05 '13 17:11

user1652427


1 Answers

DBCC FREEPROCCACHE is for the plan cache (query compilation) which is likeky to be a small gain in your query, not the page buffer data cache which is a big improvement on IO. To be consistent, you need to clear the buffer cache, after having done a checkpoint in your database, with

CHECKPOINT
DBCC DROPCLEANBUFFERS
like image 177
ARA Avatar answered Oct 05 '22 06:10

ARA