SQL Server Stored Procedures: do they queue up?

情到浓时终转凉″ 提交于 2021-02-08 23:36:31

问题


I should probably be able to find an answer to this but my Google-fu is weak today. When I have the same stored proc called multiple times by a web app, do the calls queue up or do they run independently?


回答1:


Depends on the isolation level of what the stored procedure is doing. If the isolation level is set to READ UNCOMMITTED for all the transactions in the SP, there is no protection, and multiple threads can be performing the same transaction at the same time.

If it's set to a higher isolation level, then other threads may be locked out of the resources that your SP is dealing with until the transaction is completed, effectively "queuing" the other SP threads.

There is no explicit stored procedure queue though. As long as your database has free connections and resources available, it will spawn threads to satisfy requests.




回答2:


Both. Each invocation of a stored procedure (more precisesly each request sent by the client) creates a task in SQL, visible in sys.dm_os_tasks. Tasks are assigned to schedulers (sys.dm_os_schedulers) and wait until a worker (sys.dm_os_workers) is available to run them. If the system is very busy then the tasks will queue up, and this is visible in the work_queue_count column of sys.dm_os_schedulers. For more details see Thread and Task Architecture.

Under normal operations though the queueing effect is not visible as the system immedeatly picks up the submitted tasks and start running them.

Clients can submit only one request per connection (MARS is the exception, not the rule). So from a client point of view, he must queue requests when using a connection, but this is hidden in the program control flow (ie. it must wait for a request to return before submitting a new one).




回答3:


They would run independently. If they queued up, that would cause some massive scalability problems if you had a busy system that used lots of stored procedures.

This assumes of course that the sprocs aren't locking resources in a way that would cause one call to have to wait for another to finish.



来源:https://stackoverflow.com/questions/1467353/sql-server-stored-procedures-do-they-queue-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!