2012-02-18 31 views
-1

我有一個顯示帖子的網站。我希望網站的滾動行爲像twitter一樣 - 向下滾動將顯示越來越多的帖子,無休止地。 假設我有以下表格:使用存儲過程作爲twitter滾動時顯示行

一個郵政表,可容納所有帖子。每個帖子都與一個人相關

CREATE TABLE [dbo].[Post](
    [Id] [bigint] IDENTITY(1,1) NOT NULL, 
    [PersonId] [int] NOT NULL, 
     [PublishDate] [datetime] NOT NULL, 
CONSTRAINT [PK_Post] PRIMARY KEY CLUSTERED 
(
    [Id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

一個郵政標籤表,用於保存每個帖子的所有相關標籤。

CREATE TABLE [dbo].[PostTag](
    [PostId] [bigint] NOT NULL, 
    [TagId] [int] NOT NULL, 
CONSTRAINT [PK_PostTag] PRIMARY KEY CLUSTERED 
(
    [PostId] ASC, 
    [TagId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

對於網站的每個用戶,該UserPersonStatistics表保存他表現出一個人的相關職位感興趣的次數。

CREATE TABLE [dbo].[UserPersonStatistics](
    [UserId] [bigint] NOT NULL, 
    [PersonId] [int] NOT NULL, 
    [Counter] [bigint] NOT NULL, 
CONSTRAINT [PK_UserPersonStatistics] PRIMARY KEY CLUSTERED 
(
    [UserId] ASC, 
    [PersonId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

對於該網站的每個用戶,UserPostStatistics表保存他對帖子表現出興趣的次數。

CREATE TABLE [dbo].[UserPostStatistics](
    [UserId] [bigint] NOT NULL, 
    [PostId] [bigint] NOT NULL, 
    [Counter] [bigint] NOT NULL, 
CONSTRAINT [PK_UserPostStatistics] PRIMARY KEY CLUSTERED 
(
    [UserId] ASC, 
    [PostId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

對於該網站的每個用戶,UserTagStatistic表包含他表現出對標籤相關帖子感興趣的次數。

CREATE TABLE [dbo].[UserTagStatistics](
    [UserId] [bigint] NOT NULL, 
    [TagId] [int] NOT NULL, 
    [Counter] [bigint] NOT NULL, 
CONSTRAINT [PK_UserTagStatistics] PRIMARY KEY CLUSTERED 
(
    [UserId] ASC, 
    [TagId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
     ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

我需要的是爲每個用戶返回每次35個不同崗位的存儲過程,一個可以「記住」最後的35後,所以它不會再返回相同的職位,以及35個職位應該由的: 15職位最熱門的標籤(UserTagStatistics) 15職位最受歡迎的人(UserPersonStatistics) 5個最熱門的帖子(UserPostStatistics)

的一個問題是,該程序,每次都應返回35級不同的職位。 還有一個問題是,帖子可以返回一次作爲最熱門的帖子,一次是最受歡迎的標籤的帖子,一次可以作爲最受歡迎的人的帖子。這篇文章應該算一次,而不是三次。 存儲過程的性能至關重要。

我知道它是一個非常複雜的問題。 任何想法表示讚賞。

kruvi

回答

1

添加「LastViewed」時間字段的所有表,然後使用這樣的PROC。爲了提高性能,只需確保在三個表中的每一個上都有一個UserID + LastViewed + Counter和UserID + PersonID的索引,並且它應該尖叫。實際上,由於UserID + LastViewed + Counter實際上是整個表格,如果可能的話,我建議您在每個表格上設置clustered index,以避免創建基本上與原始表格大小相同的第二個索引。

create proc GetInfo(@UserId bigint) as 
    begin 
     update userpersonstatistics 
     set 
      lastviewed=getdate() 
     where 
      [email protected] and personid in 
       (
       select top 15 personid from userpersonstatistics 
       where 
        [email protected] and 
        (
        lastviewed is null or lastviewed != 
         (select max(lastviewed) from userpersonstatistics 
         where [email protected]) 
        )  
       order by counter desc 
       ) 


     select * from UserPersonStatistics 
       where [email protected] and LastViewed = 
      (select max(lastviewed) from UserTagStatistics) 

     --**Repeat the above code for UserPostStatistics and UserTagStatistics 
    end 
修訂PROC

基於輸入:

create proc GetInfo(@UserId bigint) as 
    begin 
     declare @lastviewed datetime 
     declare @results TABLE 
     (
      StatType varchar(10), 
      Counter int, 
      PostID 
     ) 

     set @lastviewed = getdate() 

     --Person 
     insert into @results(StatType,Counter,PostID) 
     select 
      'Person',counter,PostID 
     from 
      UserPersonStatistics 
     where 
      [email protected] and personid in 
       (
       select top 35 personid from userpersonstatistics 
       where 
        [email protected] and 
        (
        lastviewed is null or lastviewed != 
         (select max(lastviewed) from userpersonstatistics 
         where [email protected]) 
        )  
       order by counter desc 
       ) 


     --Post 
     insert into @results(StatType,Counter,PostID) 
     select 
      'Post',counter,PostID 
     from 
      UserPostStatistics 
     where 
      [email protected] and Postid in 
       (
       select top 35 Postid from userPoststatistics 
       where 
        [email protected] and 
        (
        lastviewed is null or lastviewed != 
         (select max(lastviewed) from userPoststatistics 
         where [email protected]) 
        )  
       order by counter desc 
       ) 


     --Tag 
     insert into @results(StatType,Counter,TagID) 
     select 
      'Tag',counter,TagID 
     from 
      UserTagStatistics 
     where 
      [email protected] and Tagid in 
       (
       select top 35 Tagid from userTagstatistics 
       where 
        [email protected] and 
        (
        lastviewed is null or lastviewed != 
         (select max(lastviewed) from userTagstatistics 
         where [email protected]) 
        )  
       order by counter desc 
       ) 


     --At this point you could have 105 rows of the various types (35*3). 
     --You can use whatever algorithm you need to decide the top 35. 
     --That may include some weighting. 
      --You may want to consider using the Rank() function. 
    end 

如果你的算法應該考慮從每個類別前的#2#1頂櫃,看一看的Rank() function

+0

不應該將Lastviewed列添加到Post表中嗎?你的代碼會給我15個最匹配的人,標籤和帖子(總共45個,可能包含重複的帖子),但是當用戶向下滾動併發送第二個帖子請求時,新帖子仍然可以對於同樣的15個人或標籤,而只是具有較低櫃檯的職位。你的代碼不會像以前那樣爲相同的人/標籤返回帖子,不是嗎?我希望我很清楚...... – kruvi 2012-02-19 07:11:26

+0

假設我在UserPersonStatistics表中有一個具有計數器等於10的人X,並且具有計數器的第二個人Y等於1.個人X在Post post中有100個帖子,並且人有40個帖子。代碼應該返回人X的所有100個帖子,並且只返回人Y的返回帖子,或者至少在人X和Y之間保持10比2的比例。上帝幫助我:) – kruvi 2012-02-19 07:21:04

+0

如果沒有PostStats會發生什麼或TagStats計數器?那麼所有35個帖子都需要來自PersonStats?或情況#2:如果我們發現15個PersonStats,15個PostStats和5個TagStats,但它們都是相同的15個帖子呢?你只返回15或返回並從每個統計類別中檢索更多?哪個Stats優先,然後讓我們到35?如果說,我已經找到34個獨特的帖子並且需要多一個帖子,我是否可以從PostStat,TagStat或PersonStat中獲得下一篇文章? – sisdog 2012-02-19 08:27:18

相關問題