[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

MS SQL Cache

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
sfletcher1959
Asked:
sfletcher1959
  • 9
  • 4
1 Solution
 
lcohanDatabase AnalystCommented:
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
 
lcohanDatabase AnalystCommented:
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
 
sfletcher1959Author Commented:
I completely understand the cache flush on SQL Server restart, but why it it happening every day without a server restart?
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
DrewKjellCommented:
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
 
lcohanDatabase AnalystCommented:
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
 
lcohanDatabase AnalystCommented:
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
 
lcohanDatabase AnalystCommented:
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
 
sfletcher1959Author Commented:
Any way to tell where the cache is being stored.  I am not a DBA so not sure where I would look.
0
 
sfletcher1959Author Commented:
Also how to increase the cache size.
0
 
lcohanDatabase AnalystCommented:
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
 
lcohanDatabase AnalystCommented:
"......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
 
sfletcher1959Author Commented:
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
 
lcohanDatabase AnalystCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now