创建一个可扩展的数据库模式来存储高尔夫球比分

azg*_*fer 4 mysql sql database-design database-schema

我正在尝试设计一个数据库来存储我所有的朋友和我的高尔夫成绩。您可能知道,高尔夫球比分由 18 洞个人比分组成。我可以想到两种设计模式的方法:

  1. 创建一个表,每个洞都有一列(例如 h1 到 h18),该表具有引用其他表的 FK player_id、round_id 和 course_id。它有一个总列,它是 h1 到 h18 列的总和。如果我更改了一个洞分数,我将需要手动更新总列。

  2. 创建一个表,其中包含一列球洞得分、一列球洞索引、一列 player_id、course_id 和 round_id。要获得一轮的总分,我需要对 round_id、player_id 进行 SUM 查询。

目前,数据库可能会存储少于 20 人的分数,因此任何一种方法都应该没问题。但是如果我想存储 20,000 人的分数,哪种方法更具可扩展性呢?

我正在使用 MySQL 5 和 PHP5。谢谢。

更新> 查询示例: 1. 读取所有玩家在一轮中的 9/18 得分并建立记分卡。2. 基本统计数据,例如找到球员最后 X 轮的最低/平均/最高总分。3. 更高级的统计数据,如最近 X 轮任何洞的平均得分。

Ron*_*nis 7

我的平均分、最高分和最低分是多少?

场景一。

select (h1+h2+h3+h4+h5+h6+h7+h8+h9+h10+h11+h13+h14+h15+h16+h17+h18) / 18 as avg_score
      ,greatest(h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) as highest
      ,least(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) as lowest
  from scores
 where player_id = 1;
Run Code Online (Sandbox Code Playgroud)

对比

select avg(score) as avg_score
      ,max(score) as highest
      ,min(score) as lowest
  from scores
 where player_id = 1;
Run Code Online (Sandbox Code Playgroud)

哪个洞是我最糟糕的?

场景 2。

select case when h1 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H1'
            when h2 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H2'
            when h3 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H3'
            when h4 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H4'
            when h5 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H5'
            when h6 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H6'
            when h7 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H7'
            when h8 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H8'
            when h9 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18)  then 'H9'
            when h10 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H10'
            when h11 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H11'
            when h12 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H12'
            when h13 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H13'
            when h14 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H14'
            when h15 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H15'
            when h16 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H16'
            when h17 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H17'
            when h18 = greatest(h1,h2,h3,h4,h5,h6,h8,h8,h9,h10,h11,h12,h13,h14,h16,h17,h18) then 'H18'
        end as hole_highest_score      
  from scores
 where player_id = 1;
Run Code Online (Sandbox Code Playgroud)

对比

select hole, score
  from scores s1
 where player_id = 1
   and score = (select max(score)
                  from scores s2
                 where s2.player_id = s1.player_id)
Run Code Online (Sandbox Code Playgroud)

我会在任何一天使用场景 2 :)