4

我一直在开发一个应用程序,并且遇到了一种情况,我想对当前数据进行快照

例如,在此应用程序中,用户将拥有不同的统计数据并能够输入匹配项。他们在比赛中的排名取决于他们的数据。确定比赛后,应用程序将提取用户的所有当前统计数据并确定他们的分数以查看谁获胜。

现在,在比赛结束后,我希望用户能够查看过去的比赛,当我想显示比赛时参与者的分数时,问题就出现了。我认为存储如下结构的数组是可以接受的:

array(
 array(username, points),
 array(username, points),
 etc.
)

现在规范化数据通常可能是最佳实践,但在这种情况下:

  • 一场比赛可以有225名参与者。
  • 数据永远不会更新,只会读取。
  • 我认为将它放在数据库中的数组结构中可以节省我在后端代码中构造数组的时间。
  • 编辑:数据不是永久的。比赛记录将在比赛结束7天后删除。

谁能告诉我这个解决方案是否会带来任何问题?


编辑

我将在序列化数组后保存数据,所以在我的数据库中我会有一个名为“matches”的表,它会有一个名为“results”的列。

此列的行将包含序列化数组。因此,如果数组看起来像这样:

$array["a"] = "Foo";
$array["b"] = "Bar";
$array["c"] = "Baz";
$array["d"] = "Wom";

Then the row in the database would look like this:

a:4:{s:1:"a";s:3:"Foo";s:1:"b";s:3:"Bar";s:1:"c";s:3:"Baz";s:1:"d";s:3:"Wom";}
4

2 回答 2

1

This solution wouldn't pose any problems in the short term - but say you wanted to eventually add in functionality to show all of the games a user has played in, or their highest scoring games... having this data in an inaccessible-from-sql array would not allow you to have those features.

I'm thinking a table like this would be perfect:

CREATE TABLE game_scores(
    id int AUTO_INCREMENT NOT NULL PRIMARY KEY,
    game_id int,
    user_id int,
    final_score int,
    KEY(game_id),KEY(user_id)
)

At the end of every game, you'd simply insert a row for every user that was playing that round with their corresponding score and the game id. Later, you'd be able to select all of the scores for a certain game:

SELECT * FROM game_scores WHERE game_id=?

... or show all scores by a certain user:

SELECT * FROM game_scores WHERE user_id=?

etc. Have fun with it!

于 2012-09-16T05:29:21.443 回答
0

If you're really committed to the use cases you've outlined in the question along with the qualification in your comment to Sean Johnson, then I don't see any problems with your approach.

I still might qualify that by suggesting that you normalize the data if you think there's a chance you'll want to be able to mine historical information, but dumping an array into the database as a long lived (relatively speaking) sort of cache might make sense. In other words, store it in both formats, but the main line of the use case you've outlined would just hit the array format, but you'd still have the data in a queryable form if you ever wanted it.

于 2012-09-16T05:42:06.620 回答