如何实施一个系统来确定是否已达到里程碑

发布于 2024-08-27 22:28:53 字数 1085 浏览 6 评论 0原文

我有一个名为“统计”的表,

player_id team_id match_date  goal assist`  
        1       8  2010-01-01    1      1
        1       8  2010-01-01    2      0
        1       9  2010-01-01    0      5
  ...

我想知道球员何时达到里程碑(例如 100 个进球、100 个助攻、500 个进球...)
我还想知道团队何时达到里程碑。
我想知道哪个球员或球队第一、第二、第三达到 100 个进球...

我想使用带有表格的触发器来累计总数。
表player_accumulator(和team_accumulator)表将是

player_id total_goals total_assists
        1           3             6


team_id   total_goals total_assists
      8             3             1
      9             0             5 

每次在统计表中插入一行时,触发器将插入/更新player_accumulator和team_accumulator表。
此触发器还可以验证玩家或团队是否已达到包含数字的里程碑表中的里程碑

milestone
      100
      500
     1000
      ...

表player_milestone将包含玩家达到的里程碑:

player_id  stat    milestone          date
        1  goal          100    2013-04-02
        1  assist        100    2012-11-19


There is a better way to implements a "milestone" ?
There is an easiest way without triggers ?

我正在使用 PostgreSQL

I have a table named stats

player_id team_id match_date  goal assist`  
        1       8  2010-01-01    1      1
        1       8  2010-01-01    2      0
        1       9  2010-01-01    0      5
  ...

I would like to know when a player reach a milestone (eg 100 goals, 100 assists, 500 goals...)
I would like to know also when a team reach a milestone.
I want to know which player or team reach 100 goals first, second, third...

I thought to use triggers with tables to accumulate the totals.
Table player_accumulator (and team_accumulator) table would be


player_id total_goals total_assists
        1           3             6


team_id   total_goals total_assists
      8             3             1
      9             0             5 

Each time a row is inserted in stats table, a trigger will insert/update player_accumulator and team_accumulator tables.
This trigger could also verify if player or team has reached a milestone in milestone table containing numbers

milestone
      100
      500
     1000
      ...

A table player_milestone would contains milestone reached by player:

player_id  stat    milestone          date
        1  goal          100    2013-04-02
        1  assist        100    2012-11-19


There is a better way to implements a "milestone" ?
There is an easiest way without triggers ?

I'm using PostgreSQL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2024-09-03 22:28:53

我只计算得分球员以及得分球队的所有进球和助攻。

像这样在客户端(以伪代码):

function insert_stat(player_id, team_id, match_date, goals, assists)
{
  if (goals>0) {
    player_goals_before = query('select count(goal) from stats where player_id=?',player_id);
    team_goals_before = query('select count(goal) from stats where team_id=?',team_id);
  }
  if (assists>0) {
    player_assists_before = query('select count(assist) from stats where player_id=?',player_id);
    team_assists_before = query('select count(assist) from stats where team_id=?',team_id);
  }
  query("insert into stats (player_id, team_id, match_date, goal, assist)\n"
    +"values (?, ?, ?, ?, ?)", player_id, team_id, match_date, goal, assist);

  if (goals>0) {
    if ( has_milestone(player_goals_before+goals) and !has_milestone(player_goals_before) ) {
      alert("player " + player_id + " reached milestone!")
    }
    if ( has_milestone(team_goals_before+goals) and !has_milestone(team_goals_before) ) {
      alert("team " + team_id + " reached milestone!")
    }
  }
  // etc
}

不要维护里程碑表,因为这会使数据库非规范化。我认为这是一个不成熟的优化。只有当上述确实不够快时(例如,当每个player_id或team_id的统计数据超过几千行时),您才可以考虑维护里程碑表。

I'd just count all goals and assists of a player which scores, and team, which scores.

Like this on client side (in pseudocode):

function insert_stat(player_id, team_id, match_date, goals, assists)
{
  if (goals>0) {
    player_goals_before = query('select count(goal) from stats where player_id=?',player_id);
    team_goals_before = query('select count(goal) from stats where team_id=?',team_id);
  }
  if (assists>0) {
    player_assists_before = query('select count(assist) from stats where player_id=?',player_id);
    team_assists_before = query('select count(assist) from stats where team_id=?',team_id);
  }
  query("insert into stats (player_id, team_id, match_date, goal, assist)\n"
    +"values (?, ?, ?, ?, ?)", player_id, team_id, match_date, goal, assist);

  if (goals>0) {
    if ( has_milestone(player_goals_before+goals) and !has_milestone(player_goals_before) ) {
      alert("player " + player_id + " reached milestone!")
    }
    if ( has_milestone(team_goals_before+goals) and !has_milestone(team_goals_before) ) {
      alert("team " + team_id + " reached milestone!")
    }
  }
  // etc
}

Do not maintain milestone table, as this makes the database denormalized. I think this is a premature optimization. Only when the above is really not fast enough (for example when stats will have more than few thousands of rows per player_id or team_id) then you can think of maintaining milestone table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文