2011-03-14 48 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