SqlCacheDependency导致SQL Server CPU占用率高
我们遇到一个问题,即我们的 sql 服务器的 CPU 使用率跳至并保持在 100%,直到站点被关闭并重新启动。我们已经检查了代码并优化了一切,但这种情况仍在发生。
我们对缓存所做的就是运行一个查询,从 sql server 加载整个结构(约 6000 行),将其存储在缓存中,然后查询该缓存以执行我们需要在应用程序的其余部分执行的各种操作(有需要对数据执行大量递归操作,否则会对 sql 服务器造成巨大打击)。
我描述上面的内容是因为似乎在使用sql缓存依赖时,我们遇到了sql服务器上的cpu峰值。如果禁用它,即使我们仍然缓存相同数量的数据,我们也不会再遇到峰值(在 SQL 服务器或 Web 服务器上)。
有谁知道 sql 缓存依赖会导致这样的行为吗? 使用的sql服务器是SQL Server 2008 R2。 Web 服务器是 IIS 7.5,我们使用 ASP.NET 3.5。该 Web 服务器设置为集群 (x2),并且它们都指向同一个 sql 服务器。
这是加载/设置缓存的代码:
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["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);
}
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.ConnectionStrings["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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。事实证明,索引不知何故损坏或丢失。右键单击表中的索引并选择“重建”解决了问题。
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.