2011-03-14 51 views
2

我在Oracle中有一個記錄用戶事件的表。該用戶可能有很多事件。從這些事件中,我正在計算一個公式的聲望。我的問題是,在計算和返回數據時,這是做什麼最好的方法。使用視圖並使用SQL,通過獲取所有事件並計算它(與此相關的問題是當您有用戶列表並需要計算所有用戶的信譽)或其他內容時,通過代碼執行此操作。喜歡聽你的想法。聲譽公式 - 最佳方法

Comments * (.1) + 
Blog Posts * (.3) + 
Blog Posts Ratings * (.1) + 
Followers * (.1) + 
Following * (.1) + 
Badges * (.2) + 
Connections * (.1) 
= 100% 

一個例子

Comments: 

This parameter is based on the average comments per post. 

• Max: 20 
• Formula: AVE(#)/max * 100 = 100% 
• Example: 5 /10 * 100 = 50% 

最大是最大數量得到所有的百分比。希望這是有道理的。

我們正在計算訪問,所以所有獨特的訪問/成員的日期是另一個。該表包含一個事件名稱和一些元數據,並與該用戶綁定。聲譽只是使用這些事件來制定最高100%的聲譽。

85% reputation - Joe AuthorUser been a member for 3 years. He has: 
• written 18 blog posts 
o 2 in the past month 
• commented an average of 115 times per month 
• 3,000 followers 
• following 2,000 people 
• received an average like rating of 325 per post 
• he's earned, over the past 3 years: 
o 100 level 1 badges 
o 50 level 2 badges 
• he's connected his: 
o FB account 
o Twitter account 
+3

嘗試提供更多規範,如數據模型,信譽計算的複雜性... – 2011-03-14 20:15:15

+1

數據量也是 – cagcowboy 2011-03-14 20:19:18

+0

這些信息是否足夠? – 2011-03-14 20:40:21

回答

1

作爲一般的方法,我將使用PL/SQL。一個包含幾個get_rep函數的包。

function calc_rep (i_comments in number, i_posts in number, i_ratings in number, 
        i_followers in number, i_following in number, i_badges in number, 
        i_connections in number) return number deterministic is 
... 
end calc_rep; 

function get_rep_for_user (i_user_id in number) is 
    v_comments .... 
begin 
    select ..... 
    calc_rep (v_comments...) 
end get_rep_for_user; 

如果你要重新計算代表了很多用戶的很多的時候,我會考慮並行流水線功能(這應該是一個單獨的問題)。 CALC_REP是確定性的,因爲任何具有相同數字的人都會得到相同的結果。

如果評論等的數量存儲在一條記錄中,那麼調用起來很簡單。如果需要總結細節,則使用物化視圖來獲取摘要。如果他們需要從多個地方收集,則可以使用視圖來封裝聯接。

+0

我現在用VIEW方法。帶有觸發器的數據摘要的單行也可能是一個選項。我們跟蹤事件,所以即使我們刪除了這些數據,我們也可以根據參數的變化輕鬆地重新計算總數。 – 2011-03-16 06:28:07

1

是否可以在飛行中快速計算滿足要求是數據量,數據庫設計,最終計算複雜度的一個因素.....想象一下,我們可以給你一個切割乾燥的方法是不合理的。

它可能會通過存儲用於某些計算值的摘要來獲得幫助。例如,看看導致DML的東西。如果您有一個user_reputation表,那麼您的blog_post表上的觸發器可以在插入或刪除帖子時遞增/遞減user_reputation上的計數器。

如果您保持所有摘要都是最新的,那麼DML的增量成本將會很小,計算將變得簡單。

不是說這是解決方案。只是說這可能值得探討。

+0

有趣的方法。如果這不是一個動態網站,聲譽可能會改變,參數被添加或刪除,這可能是一個很好的解決方案。 – 2011-03-16 06:25:55