为了检索ID,我首先在两个后续查询中进行选择,然后进行更新。
问题是我遇到了锁定行的问题。我已经读过,将这两个语句,Select和Update放在一个存储过程中,它有助于锁定。这是真的吗?
我运行的查询是:
select counter
from dba.counter_list
where table_name = :TableName
update dba.counter_list
set counter = :NewCounter
where table_name = :TableName
问题是多个用户可能会选择同一行,也可能会更新同一行。
答案 0 :(得分:0)
表counter_list是否同时由多个客户端访问?
OLTP的最佳实践是调用将在一个事务中执行更新逻辑的存储过程。
检查表dba.counter_list是否有列table_name的索引。 还要检查它是否已锁定行级别。
答案 1 :(得分:0)
假设:
select
会返回counter
counter
值用于某些目的
考虑以下update
语句,该语句应消除同时运行select/update
逻辑的多个用户可能出现的任何竞争条件:
declare @counter int -- change to the appropriate datatype
update dba.counter_list
set @counter = counter, -- grab current value
counter = :NewCounter -- set to new value
where table_name = :TableName
select @counter -- send previous counter value to client
update
获取所需行(或页面/表格的独占锁定,具体取决于表格设计和锁定方案)您是通过SQL批处理还是存储过程调用提交上述内容取决于您和您的DBA来决定......