具有并发读写访问的表的锁定提示
我见过一些类似的问题,但没有一个能够完全解决这种情况。
我有一个在线评估应用程序,它向用户提出问题,每页一个问题,并记录他们的答案。用户可以在问题之间导航,并且他们的回答会自动保存。
当从一个页面(问题 Q1)导航到另一个页面(问题 Q2)时,数据库必须:
- 更新该用户对 Q1 的响应(如果存在)或 INSERT 对 Q1 的响应。
- 选择该用户对 Q2 的响应(如果存在)以填充页面。
[RESPONSE] 表是许多用户同时读写的争论点。但是,每个用户只能读取和写入自己的行。
这让我认为我可以安全地使用 (READUNCOMMITTED)、 和 UPDATE (NOLOCK) 。我担心的是,我不希望出现这样的情况:用户前进一个页面,然后跳回并获取更新之前的旧数据。我可以将这两个操作放在一个事务中,但是如果我使用 NOLOCK 提示,这会有什么不同吗?
我可以使用什么锁定策略来缓解该表上的争用?
我们目前使用的是 SQL2000。
I've seen a few similar questions but none that quite address this scenario.
I have an online assessment application that presents users with questions, one question per page, and records their answers. Users can navigate between questions, and their responses are automatically saved.
When navigating from one page (question Q1) to another (question Q2), the database has to:
- UPDATE this user's response to Q1 (if it exists) or INSERT a response to Q1.
- SELECT this user's response to Q2 (if it exists) to populate the page.
The [RESPONSE] table is a point of contention with many users concurrently reading and writing. However, each user will only ever read and write to their own rows.
This leads me to think I could use (READUNCOMMITTED), and UPDATE with (NOLOCK) safely. My worry is, I don't want to have the situation where a user goes forward a page, and then jumps back and gets the old data before it has been updated. I can put the two operations inside a transaction, but if I'm using NOLOCK hints will that make any difference?
What locking strategy can I use to alleviate contention on this table?
We're currently on SQL2000.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该只需要使用 rowlock 提示并确保您有适当的索引,以便可以直接查找行,而无需扫描属于不同用户的其他行。
You should just need to use the
rowlock
hint and make sure that you have appropriate indexes such that the rows can be seeked to directly without needing to scan other rows belonging to different users.