Linq-to-SQL 和 sp_reset_connection
我有一个 .NET 4.0 Winform 和一个 .NET 4.0 Windows 服务,它们都通过 LINQ to SQL 连接到 SQL 2005/2008 数据库。它在我们的测试环境中运行良好且快速,具有生产数据的完美克隆,但在生产环境中,它运行非常缓慢,并且 CPU 使用率和带宽使用率较低。我还注意到每天有数百次 SQL 超时,即使对于索引良好的数据库上的最小查询也是如此。于是我启动了 Profiler...
我发现在负载下的 10 分钟捕获期间,sp_reset_connection 占 SQL CPU 总持续时间的三分之一和所有 SQL 调用总数的 90%。
- 我尝试过禁用 &启用连接池并调整连接字符串中允许的连接数和连接超时。没有一个有任何效果。
- 当我遇到 LINQ 查询时,我一直用 ADO.NET 查询替换它们。这些旧的 ADO.NET 查询永远不会超时。只有 LINQ 的。
- 我注意到该服务器上存在其他主要性能问题,但我不确定如何与客户的系统管理员处理该主题。
我对运行该服务的应用程序服务器具有管理员访问权限。我几乎无法访问运行 Winform 的终端服务器,也无法访问 SQL 服务器。
是什么原因导致 sp_reset_connection 如此频繁地运行?
- 有没有办法绕过这些调用而不将所有 LINQ 从我的应用程序中删除?
- 有没有办法减少对该存储过程的调用次数?
- 有没有办法减少 SQL Server 执行这些调用所需的处理器时间?
- 如果我禁用池并用空的存储过程替换该存储过程,是否会弄乱其他任何事情?
I have a .NET 4.0 Winform and a .NET 4.0 Windows Service which both connect to a SQL 2005/2008 database through LINQ to SQL. It runs nice and fast on our test environment with a perfect clone of production data, but on the Production environment, it runs really slowly and has low CPU usage and bandwidth usage. I also noticed hundreds of SQL timeouts a day, even for the smallest queries on a well-indexed database. So I fired up the Profiler...
I found that sp_reset_connection accounted for one third of total SQL CPU duration and 90% of all total SQL calls during a 10-minute capture under load.
- I have tried disabling & enabling connection pooling and fiddling with the number of allowed connections and connection timeouts in the connection string. None have had any effect.
- I have been replacing my LINQ queries with ADO.NET queries as I come across them. These old ADO.NET queries never time out. Only the LINQ ones.
- I have noticed other major performance issues on that server, but I'm not sure how to approach the subject with the customer's sysadmin.
I have Admin access on the Application server the service runs on. I have next to no access on the terminal server where they run the Winform, nor to the SQL server.
What causes sp_reset_connection to run so often?
- Is there a way to circumvent these calls without tearing all the LINQ out of my application?
- Is there a way to reduce the number of calls to this stored procedure?
- Is there a way to reduce the amount of processor time the SQL server needs for these calls?
- Will I mess anything else up if I leave pooling disabled and replace that stored proc with, say, an empty one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了一些与此相关的其他页面,其中一个建议是这样的:
对于重置连接,请在使用 DataContext 连接之前打开它,并在最后关闭它。
Found some other pages about this, one suggested this:
For your connection reset, open your DataContext connection before working with it and close it at the end.
当sqlconnection返回到连接池时会发生sp_reset_connection,现在这应该不是问题。
但现在的问题是为什么会超时?是sql服务器无法处理事务量还是连接池耗尽,从未使用过Linq-to-sql,但请确保在处理完对象后处理掉所有可以处理的东西...
编辑:连接池存在的原因是删除它可能会恶化你的性能,删除“sp_reset_connection”会给你带来奇怪的错误,因为数据将被转移到连接的下一个用户......
以降低sp_reset_connection的数量您能做的唯一方法是尝试为尽可能多的查询重复使用相同的连接!
sp_reset_connection occurs when a sqlconnection returns to the connection pool, now this should not be a problem.
but now the question is why do you get timeouts? is it the sql server that can't cope with the amount of transactions or is it that the connection pool depletes, never used Linq-to-sql but make sure you dispose every thing you can dispose when your done with the objects...
Edit: the connection pool is there for a reason it removing it will likly worsen your performance and removing "sp_reset_connection" will give you weird errors as data will be carried over to the next user of the connection...
to lower the amounts of sp_reset_connection the only way you can do is try to reuse the same connection for as many queries as you can!