SQL Server 2008 存储过程同时运行导致延迟
我遇到一个问题,似乎当从同一应用程序的两个不同实例同时运行相同的存储过程时,它超时了,并且想知道是否可以采取任何措施来解决它?
我相信问题在于 SQL Server 2008 处理自身的方式,锁定行和执行 SP...我不太了解这些事情。该应用程序使用 ADODB.Command
来执行 SP。
我有一个 VB6 exe (App.exe
),在一台服务器上运行多次。此应用程序调用数据库上的存储过程,该过程返回该应用程序的下一个序列号。该序列号字段对于应用程序的实例是唯一的 - 对于正在运行的应用程序的每个实例,表 (tbl_SequenceNos
) 中有 1 行。
例如,假设我们正在运行: App[a].exe
和 App[b].exe
tblSequenceNos
看起来像:
iAppNo| iNextSequenceNo
a | 1234
b | 4567
存储过程获取下一个序列号相对简单:
CREATE PROEDURE GetNextSequenceNo (@AppNo varchar(1), @NextSequenceNo int output)
AS
BEGIN
DECLARE @TempSequenceNo int
SELECT @NextSequenceNo = iNextSequenceNo
FROM tblSequenceNos
WHERE iAppNo = @AppNo
@NextSequenceNo = @NextSequenceNo + 1
UPDATE tblSequenceNos
SET iNextSequenceNo = @NextSequenceNo
WHERE iAppNo = @AppNo
END
当 App[a].exe
和 App[b].exe
都尝试运行此过程来获取其 NextSequenceNo
值,它们挂起大约30 秒(ADO 超时?)。
因为每个应用程序从不查看彼此的行,所以我认为这可以同时工作而无需指定特殊的锁定。我有什么遗漏的吗?我想也许我需要指定仅锁定行,而不是整个表或页面? - 我不知道sql2008默认做什么。
非常感谢任何帮助! 先感谢您 安德鲁
I have a problem where it seems that when running the same stored procedure at the same time from two different instances of the same application it is timing out, and wondered if there was anything I could do to resolve it?
I believe the problem is in the way SQL Server 2008 handles itself, locking rows and executing the SPs...things I don't really know a whole lot about. The app uses ADODB.Command
to execute the SP.
I have one VB6 exe (App.exe
), running on the one server multiple times. This app calls a stored proc on the database which returns the next sequence number for that app. This sequence number field is unique for the instance of the application - there is 1 row in a table (tbl_SequenceNos
) for each instance of the running application.
So for example, say we have running: App[a].exe
and App[b].exe
tblSequenceNos
looks like:
iAppNo| iNextSequenceNo
a | 1234
b | 4567
The stored procedure to get the next sequence number is relatively simple:
CREATE PROEDURE GetNextSequenceNo (@AppNo varchar(1), @NextSequenceNo int output)
AS
BEGIN
DECLARE @TempSequenceNo int
SELECT @NextSequenceNo = iNextSequenceNo
FROM tblSequenceNos
WHERE iAppNo = @AppNo
@NextSequenceNo = @NextSequenceNo + 1
UPDATE tblSequenceNos
SET iNextSequenceNo = @NextSequenceNo
WHERE iAppNo = @AppNo
END
When both App[a].exe
and App[b].exe
try to run this procedure to get their NextSequenceNo
value, they are hanging for about 30Secs (ADO timeout?).
Because Each app never looks at the each others row, I thought that this would work concurrently without specifing an special Locking. Is there something I am missing? I thought perhaps I need to specify to lock the row only, not the whole table or Page? - I do not know what sql2008 does by default.
Any help is greatly appreciated!
Thank you in advance
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的过程不是线程安全的,并且会产生不正确的结果,因为在选择和更新之间多个线程可能会获得相同的序列号。
还要确保您的 iAppNo 列已建立索引。 (这意味着仅此列上的索引或此字段是索引中第一个字段的索引)
Your procedure is not thread safe and will produce incorrect results because between the select and the update multiple threads might get the same sequence nr.
Also make sure your iAppNo column is indexed. (This means an index on this column only or an index where this field is the first field in your index)