开发者

High CPU Usage on SQL Server Caused by SqlCacheDependency

开发者 https://www.devze.com 2023-04-05 10:09 出处:网络
We are experiencing an issue where our sql server where cpu usage jumps to and remains at 100% until the site is taken down and restarted.We have gone through the code and optimized everything we can,

We are experiencing an issue where our sql server where cpu usage jumps to and remains at 100% until the site is taken down and restarted. We have gone through the code and optimized everything we can, and this is still happening.

What we do with cache is run a query that loads an entire structure (~6000 rows) from sql server, store that in cache, and query that cache to perform the various operations we need to perform throughout the rest of the application (there are a lot of recursive operations that need to be performed on the data and it would be a huge hit to the sql server otherwise).

I describe the above because it seems that when sql cache dependency is used, we encounter the cpu spike on the sql server. If it is disabled, we no longer encounter the spike (on the sql server or the web server) even though we are still caching the same amount of data.

Does anyone have any idea what about sql cache dependency could cause behavior like this? The sql server used is SQL Server 2008 R2. The web server is IIS 7.5 and we used ASP.NET 3.5. This web servers are set up as a cluster (x2), and they both point to the same sql server.

This is the code that loads/sets up the cache:

using (SqlConnection cn = new SqlConnection(ConfigurationManager.Connectio开发者_开发知识库nStrings["MyConnectionString"].ConnectionString))
{
    cn.Open();

    string query =
        @"SELECT 
            id, 
            parentid, 
            field1, 
            field2, 
            field3, 
            field4, 
            field5
        FROM 
            dbo.theTableWithDataInIt";

    SqlCommand cmd = new SqlCommand(query, cn);
    cmd.Notification = null;
    cmd.NotificationAutoEnlist = true;
    SqlCacheDependencyAdmin.EnableNotifications(
        ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);

    if (!SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(
         ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString)
         .Contains("dbo.theTableWithDataInIt"))
    {
        SqlCacheDependencyAdmin.EnableTableForNotifications(
                    ConfigurationManager.ConnectionStrings["MyConnectionString"].
        ConnectionString, "dbo.theTableWithDataInIt");
    }

    SqlCacheDependency dependency = new SqlCacheDependency(cmd);
    cmd.ExecuteNonQuery();

    //
    // Get Cache Data is a function that returns a DataSet with the data to be added to cache
    //
    Cache.Insert("mycache", GetCacheData(), dependency);
}


The problem was resolved. It turns out the indexes somehow became corrupt or lost. Right clicking on the index in the table and selecting "Rebuild" solved the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消