简体   繁体   中英

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.

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. 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.

Is there anything out of the box in SQL server that can help me with this? 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!

Thanks, Andrew

Tips to keep in mind:

  • First, add WITH (NOLOCK) to tables where you can do it. 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
  • Use sp_who2 while investigating in production server. 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. You will be able to identify SUSPENDED selects
  • Use sp_lock to see which objects exactly are locked

Locked types can be:

  • MD - metadata lock
  • DB - database lock
  • TAB - table lock
  • PAG - page lock

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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