【发布时间】:2009-08-13 04:00:10
【问题描述】:
假设我在网站上有一篇文章,我想跟踪文章的浏览量。在 Articles 表中,有 PK ID - int、Name - nvarchar(50) 和 ViewCount - int。每次查看页面时,我都会增加 ViewCount 字段。我担心更新字段时发生碰撞。我可以在存储过程中运行它,其中包含如下事务:
CREATE PROCEDURE IncrementView
(
@ArticleID int
)
as
BEGIN TRANSACTION
UPDATE Article set ViewCount = ViewCount + 1 where ID = @ArticleID
IF @@ERROR <> 0
BEGIN
-- Rollback the transaction
ROLLBACK
-- Raise an error and return
RAISERROR ('Error Incrementing', 16, 1)
RETURN
END
COMMIT
我担心我最终会在此模型中不计算 PageViews。另一种可能的解决方案是日志类型的模型,我实际记录文章的浏览量,并使用函数和视图的组合来获取有关文章浏览量的数据。
【问题讨论】:
标签: sql tsql transactions