Solved

MS SQL Cache

Posted on 2011-09-09
14
394 Views
Last Modified: 2012-05-12
We purchased an application that has a Microsoft SQL 2005 database.  Our in-house application has an API to interrogate the SQL database on the purchased application.  Each morning the first transaction against the SQL database runs about 10 minutes.  Each subsequent transaction runs between 0 and 4 seconds.  Also if we reboot the server with the SQL database and try to hit it for the first time it also runs for about 10 minutes.  The database is only about 100 MB in size.
My thought was that there may be some caching that occurs when the database is opened and that is why it runs very quickly after the first time.  If this is the case, does it have some kind of lease, that expires overnight and must re-cache each morning?  Is this something that can be set to re-cache only on server reboot.  
This first database hit each morning is a pain!
0
Comment
Question by:sfletcher1959
  • 9
  • 4
14 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 36511733
Like any other database SQL has data cache and object cache where data respectively query plans are kept and they are 100% flushed by a SQL server restart.
For more details about caching please see:

Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005
http://technet.microsoft.com/en-us/library/cc966425.aspx

Understanding Query Plan Cache in SQL 2005 SP2 - why it's changed from SQL 2005 RTM/SP1?
http://blogs.technet.com/b/vipulshah/archive/2007/09/24/understanding-query-plan-cache-in-sql-2005-sp2-why-it-s-changed-from-sql-2005-rtm-sp1.aspx

0
 
LVL 39

Expert Comment

by:lcohan
ID: 36511737
Sorry I hit submit too soon without the link below:
Caching in ASP.NET with the SqlCacheDependency Class
http://msdn.microsoft.com/en-us/library/ms178604.aspx
0
 

Author Comment

by:sfletcher1959
ID: 36511777
I completely understand the cache flush on SQL Server restart, but why it it happening every day without a server restart?
0
 
LVL 9

Accepted Solution

by:
DrewKjell earned 500 total points
ID: 36511874
The cache in SQL I believe only stores a query plan for a certain amount of time or up to a certain number of cached plans.  

Drew
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36511905
There must be a process either in SQL on .NET that is causing the cache to be flushed - either data or plan or both due to for example a sp_recompile on the table(s) from where you get the data. By the size of your DB the cache should not be cleared unless explicitely asked for but..is this the only database on that SQL Server? May sound silly but ask yourself if there's enough memory available for SQL (and how it is configured) if this is not the only DB and app on that box.
BTW - how is your SQL code built/executed from .NET app? Is it a SQL Stored Procedure? If it's not than I suggest you can try put your code in a SP and use that SP instead.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36511923
The cach in SQL is not cleared based on any schedule if sufficient memeory is available however you can force it to clear - plan and data - by issuing a sp_recompile on the table(s) used by the query.
Other than that if data cardinality is changing a lot (not sure what the treshhold is) but if you have a masive import/delete on the table(s) than the data/plans cache need to be rebuild obviously.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36512013
here are a few other articles that may be useful to identifiy your caching issue:

Analyzing the SQL Server Plan Cache
http://www.mssqltips.com/sqlservertip/1661/analyzing-the-sql-server-plan-cache/

Check adhoc sql cache is bigger than procedure cache and clear only adhoc sql cache
http://sqlserverpedia.com/blog/sql-server-bloggers/check-adhoc-sql-cache-is-bigger-than-procedure-cache-and-clear-only-adhoc-sql-cache/

Execution Plan Caching and Reuse
http://msdn.microsoft.com/en-us/library/ms181055(v=SQL.90).aspx
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:sfletcher1959
ID: 36512376
Any way to tell where the cache is being stored.  I am not a DBA so not sure where I would look.
0
 

Author Comment

by:sfletcher1959
ID: 36512441
Also how to increase the cache size.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36512457
What do you mean by "where the cache is being stored" this is not a flder on your server or anything like that - this is SQL Server internal cache.

How to Interact with SQL Server’s Data and Procedure Cache
http://www.sql-server-performance.com/2004/data-cache/
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36512469
"......its already been answered on this thread a number of times.  You can not set, change, or otherwise manipulate the plan cache memory size in SQL Server.  If you choose to reject that simple truth and care to waste time looking for another answer, I can't stop you, but there is no magic behind this, and no button you can press to make any kind of change. "



http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/e8b07ee5-a715-4ed4-92a2-84757defae6e
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36512505
0
 

Author Comment

by:sfletcher1959
ID: 36512921
No need to get nasty lcohan.  Obviously, I didn't understand.  Which means either I didn't pay attention or you did a lousy job of explaining.  
0
 
LVL 39

Expert Comment

by:lcohan
ID: 36513032
Pardon?
What do you consider nasty in all my postings above?
All I tried was to help and make you understand how SQL caching works including the fact that in one of the links I gave the solution (not the code!) of how to workaround the problem you are having each morning!
[...]
" that expires overnight and must re-cache each morning?  Is this something that can be set to re-cache only on server reboot.  This first database hit each morning is a pain!"
[...]

if you have good eyes and ears - you will find it.
Good luck - without any intention to be "nasty"...
 
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now