维护视频文件的ViewCount数量

时间:2013-03-08 10:19:01

标签: sql-server database database-design

我需要有关架构更改的帮助。我有一个存储视频的视频表,我想知道视频的实际ViewCount

这是我现在拥有的视频表架构:

CREATE TABLE dbo.VideoFiles(
VideoId     int IDENTITY(1, 1) PRIMARY KEY,
VideoTitle  nvarchar(64) NOT NULL,
VideoDescription nvarchar(2048),
PublishDate date,
Duration    time,
VideoUrl    nvarchar(256),
--ViewCount bigint,
UpdateTime  datetime default CURRENT_TIMESTAMP )

我可以有一个ViewCount列,每次启动视频文件时都会增加+1,这看起来很简单。但是,如何在视频视图中管理唯一性?

User1 view Video1 = Video1 Count =>1  
User2 view Video1 = Video1 Count =>2  
User1 view Video2 = Video2 Count =>1  
User1 view Video1 = Video1 Count =>2 (not increased as he already seen this video) 

谢谢,
PRABHAT

2 个答案:

答案 0 :(得分:1)

所需的结构可能如下所示

CREATE TABLE [dbo].[ViedeoViews](
    [VideoID] [int] NOT NULL,
    [UserID] [int] NOT NULL,
 CONSTRAINT [PK_ViedeoViews] PRIMARY KEY CLUSTERED 
(
    [VideoID] ASC,
    [UserID] ASC
) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ViedeoViews]  WITH CHECK ADD  CONSTRAINT [FK_ViedeoViews_VideoFiles] FOREIGN KEY([VideoID])
REFERENCES [dbo].[VideoFiles] ([VideoId])
GO

ALTER TABLE [dbo].[ViedeoViews] CHECK CONSTRAINT [FK_ViedeoViews_VideoFiles]
GO



Create View V_VideoFiles as
Select dbo.VideoFiles.*,(Select COUNT(*) from ViedeoViews vv where vv.VideoID=VideoFiles.VideoID) as ViewCount
from VideoFiles

答案 1 :(得分:1)

创建表

UserId int/UniqueIdentifier
VideoId Int
TimeOfView Datetime
SessionId int/UniqueIdentifier

这将对您有所帮助。您将能够从业务角度给出每个答案。与总观看次数,观看次数/日期或每日独立访问量一样。同样的一周,几个月和一年。也可以为UserVisit添加更多属性。