sql server 2008 memory caching - sql-server-2008

I have a service that is running and connected to sql server 2008 database, the problem is that i have queries that takes a long time when run for the first time, but when cached it is finishing very fast. Does SQL server 2008 makes automatic clear cache every period of time?

SQL Server will not release memory unless there is memory pressure on the server or you explicitly tell it to.
See Microsoft support:
http://support.microsoft.com/kb/321363
Another cause could be that other database objects which need to be put in memory are pushing the ones you are using out of the buffer. In this case more memory allocated to the instance or more efficient queries will help.
So either there is memory pressure from other applications on the server or you do not have enough memory allocated to the instance for your current workload, but there is not regular scheduled process per se that cleans out SQL Server memory buffers.

Related

How does cache work when MySQL is remote?

I have been working on the server migration of a legacy ecommerce application using PHP 5.6.
The switch involved two Dedicated 32 servers from Linode.
One server is for NginX + PHP and the other is for MySQL only.
The legacy application leverages memcached.
After the switch, I can see a heavy internal traffic caused due to private inbound and outbound connections.
So far this element didn't cause any problem on performance.
However, I was under the impression that the queries would be cached on the local machine, and not on the remote.
Because if the query is cached on the remote host, it sill has to transmit the result set over the private network, instead of retrieving from RAM or the local SSD.
Am I assuming this wrong?
It may be that I am missing the point where the private inbound traffic is more beneficial for overall performance when compared to a local cache.
MySQL has a feature called the Query Cache, but this caches query result sets in the mysqld server process, not on the client. If you run the exact same query again after the result has been cached in the Query Cache, it will copy the result from the Query Cache and avoid the cost of running the query again. But this will not avoid the time to transfer the result across the network from mysqld to your PHP application.
Also keep in mind that the MySQL Query Cache is being deprecated and retired.
Alternatively, your application may store data from query results in memcached, but typically this would be done by the application code (I know there are UDF's to read and write memcached from MySQL triggers, but this is a bad idea).
If your memcached service is not on the same host as your PHP code, it would result in network transfer twice: Once when querying the data from MySQL the first time, then again transferring the data into memcached, then later every time you fetch the cached data out of memcached.
PHP also has some features to do in-memory caching, such as APCu. I don't have any experience with this, and it's not clear from a brief scan of the documentation where it stores cached data.
PHP is designed to be a "shared nothing" language. Every PHP request has its own data, and data doesn't normally last until the next request. This is why a cache is typically not kept in PHP memory. Applications rely on either memcached or the database itself, because those will hold data longer than a single PHP request.
If you have a fast enough network, it shouldn't be a high cost to fetch items out of a cache over a network. The performance architects at a past job of mine developed this wisdom:
"Remote memory is faster than local storage."
They meant that if the data is in RAM on a server, then reading it from RAM even with the additional overhead of transferring it across a network is usually better than reading the data from persistent (disk) storage on the local host.

Rails app generating MySQL load though now database access triggered

I have a Rails 4.x application running on server A and MySQL on server B.
Using ab to do a load test of my API calls I notice that the MySQL server is showing CPU activity. So I go back to the code and check, but no SQL statements are triggered, to be sure I also deactivate all before filters, but still the MySQL server shows CPU load.
I went to MySQL and run
show processlist;
but that also shows no active SQL statements
Why would there be load on the DB server?
A Rails application initializes connection pools to the configured database on app load and also loads basic schema data for each ActiveModel defined to populate runtime mappings from the DB to instances of that model.
These connections/queries will happen as soon as you have loaded the application and running traffic.
If this is not what is responsible for the activity on your database server, you will need to use other tools to see what is responsible. For example, NewRelic's system monitoring tools are great for snapshotting CPU/memory usage over time correlated to what processes were running. This will help you rule out MySQL itself using resources vs. other things running on the DB server.
According to this article, storage engines like Innodb may have their own per thread and/or global memory allocations which is probably accounting for the CPU overhead. If this is a stock (non-tuned) MySql install, you're probably just seeing baseline CPU activity. The article mentions a number of places to look that might indicate areas that can be tuned to reduce this footprint.

SQL Server 2008, recovering RAM memory

Is there a way to reset the memory used by SQL Server 2008 R2 to what it would be if I restarted the service? (but I don't want to restart the service)
I tried using
Checkpoint -- Write dirty pages to disk
DBCC FreeProcCache -- Clear entire proc cache
DBCC DropCleanBuffers -- Clear entire data cache
but I always free up more memory by restarting the service.
You can use the sp_configure procedure to change the max server memory (MB) configuration setting. SQL Server will adjust to the new setting without a restart.
You should configure this setting with the desired value then leave it if you find that SQL Server is hogging memory needed by other processes.
I found the excellent webcast Mission-Critical SQLCLR, which explains that the max mem setting in SQL Server does not include memory used by SQLCLR.
To find out how much memory is actually used by SQLCLR, you can run:
select *
from sys.dm_os_memory_objects
where [type] like '%CLR%'

MS SQL 2008 cpu usage

My application uses MS SQL server 2008 and it is hosted in a Windows 2003 Enterprise Server SP2 (32 bit) 2-CPU 8 gig Ram VM machine. The application has 2 or more windows services.One of those service access the DB frequently. When the load of the DB is set 65k or something , the CPU usage hikes upto 75-95% and it doesnt seem to reduce until unless the service is stopped.
This issue we have not faced in Oracle 10 g, with the same application and same load.
How to reduce the cpu usage ?
Is there something I need to do with the application code or with the SQL server.?
Any help will be appreciated.
Thanks,
Priya.
When it accesses the database, is it logging in, doing it's work, and then logging out? If so, see if you can retain the same connection rather than tearing down each time.
To see if it is an issue with the work it is doing, run SQL Profiler against the server and look for high read counts, high cpu count or long duration queries.

SQL Server 2008 on production using 10GB memory . Is this normal?

I have a SQL Server 2008 in production environment (Windows 2003 -64 bit) and
it is consuming 10 GB memory of installed 20GB. Is this normal behavior or is there anything wrong with the configuration ?
P.S. I have hosted one web application which is used by hundreds of users concurrently everyday .
SQL Server reserves memory which is why you are seeing high peaks. It might show up as using 10GB in your Task Manager, but the real memory usage can be checked from within the Management Studio.
Also, you can establish upper and lower limits to the amount of memory (buffer pool) used by the SQL Server database engine with the min server memory and max server memory configuration options.
Check this article out http://support.microsoft.com/kb/321363
Microsoft has adopted the strategy for memory management that any unused memory is wasted memory. Microsoft's newer OS's and SQL Server versions will allocate more memory for caching, until the system requests it for other purposes.
So, what you are seeing is probably normal.
Much of that allocated memory can be released to other applications as needed. As distressing as that memory usage may seem, it is not as dire a situation as it may appear.
There is nothing wrong with that behavior, SQL is just caching your data. If there is something else you'd like to use that memory for you can configure SQL Server to use less, however, configuring it that way may make queries slower.