简体   繁体   English

如何加快并发相同查询的SQL Server 2008 R2

[英]How to speed up concurrent identical queries SQL server 2008 R2

I've inherited a solution to support based on .NET and SQL Server 2008 R2. 我继承了一个基于.NET和SQL Server 2008 R2的支持解决方案。

The architecture is such that all clients must be kept in step with each other, and this is handled by a winsock hub to which each client sends and receives notifications of changes made. 该体系结构使得所有客户端必须彼此保持同步,并且这由一个Winsock集线器处理,每个客户端向该Winsock集线器发送和接收所做更改的通知。 These notifications are in the form of a type of update and a key field. 这些通知采用更新类型和关键字段的形式。

On receipt of a notification, client windows inspect the type of update and if they think they are interested then issue a database command (Stored procedure) to retrieve the latest data for that key value. 接收到通知后,客户端窗口将检查更新的类型,如果他们认为自己有兴趣,则发出数据库命令(存储过程)以检索该键值的最新数据。 The stored procedure typically invoked links a number of tables to return a rather large object. 通常调用的存储过程链接许多表以返回一个相当大的对象。

My problem is that when many clients are active, they are calling the same stored procedure at the 'same' time and dragging perforamnce down eg a < 1 second query, when called in isolation can take c20 seconds as it presumably waits for other clients to complete. 我的问题是,当许多客户端处于活动状态时,它们会在“相同”时间调用相同的存储过程,并将性能降低(例如,小于1秒的查询),而单独调用时可能会花费大约20秒钟,因为它可能等待其他客户端完成。

Is there anything out of the box in SQL server that can help me with this? SQL Server中是否有什么可以帮助我解决的问题? I'm thinking of caching of results or ways in which I can stop one query blocking another? 我在考虑结果的缓存或可以停止一个查询阻止另一个查询的方式?

I appreciate that I should probably make some changes on the client code and notification process to pass around fully populated objects so removing the need to interrogate the database multiple times. 我很欣赏我可能应该在客户端代码和通知过程中进行一些更改以传递完全填充的对象,从而消除了多次查询数据库的需要。 However this is a legacy application with a limited lifespan, which would be a headache to upgrade in such a significant way. 但是,这是一个寿命有限的旧版应用程序,以如此重要的方式进行升级将是一件令人头疼的事情。 If there were a nice simple switch in SQL server I could use, that would buy me the time I need! 如果我可以使用SQL Server中一个简单的开关,那将为我节省我所需的时间!

Thanks, Andrew 谢谢,安德鲁

Tips to keep in mind: 注意事项:

  • First, add WITH (NOLOCK) to tables where you can do it. 首先,将WITH(NOLOCK)添加到可以执行此操作的表中。 Remember NOLOCK is a hint for READUNCOMMITED which means it won't read pending changes already in process but it will prevent to wait until a table is released 记住NOLOCK是READUNCOMMITED的提示,这意味着它不会读取已经在处理中的挂起的更改,但是它将防止等待表被释放。
  • Use sp_who2 while investigating in production server. 在生产服务器中进行调查时,请使用sp_who2。 Launch your application or website in more than one machine in order to reproduce the scenario that causes a slower performance, get reports or operations that takes time and analyze what's going on in a Management Studio with sp_who2. 在多台机器上启动您的应用程序或网站,以重现导致性能降低的情况,获取耗时的报告或操作,并使用sp_who2分析Management Studio中的情况。 You will be able to identify SUSPENDED selects 您将能够识别“暂停”选择
  • Use sp_lock to see which objects exactly are locked 使用sp_lock查看确切锁定了哪些对象

Locked types can be: 锁定类型可以是:

  • MD - metadata lock MD-元数据锁定
  • DB - database lock DB-数据库锁定
  • TAB - table lock TAB-桌子锁
  • PAG - page lock PAG-页面锁定

If nothing works you can try analyzing query plans but you indicated performance is good when you are in an isolated environment and query this procedure. 如果没有任何效果,您可以尝试分析查询计划,但您表示在隔离环境中查询此过程时性能不错。

Another performance issue, your tempdb database can be overloaded by temporary tables if it's not optimized. 另一个性能问题是,如果未对tempdb数据库进行优化,则临时表可能会使其过载。 Run a profiler to analyze what's happening. 运行事件探查器以分析正在发生的事情。 Filter only the stored procedure you need to evaluate results. 仅过滤您需要评估结果的存储过程。

I will add more information in case something else comes up to my mind. 如果有其他想法,我将添加更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM