简体   繁体   English

保存到一个表还是创建多个表?

[英]Save into one table or create multiple tables?

I am in dilemma situation, and do not know which one is better. 我处于困境中,不知道哪个更好。 Lets say I have 100++ (or more) games on my website. 可以说我的网站上有100款(或更多)游戏。 I want to store highscore for each game. 我想为每个游戏存储高分。 Should i store all the highscore in 1 table or each game has its own table? 我应该将所有高分都存储在一张桌子中还是每个游戏都有自己的桌子?

Compare: 相比:

1 table: 1 table contains a lot of rows (data). 1个表:1个表包含很多行(数据)。 Every user play different game will submit score to this table. 每个玩不同游戏的用户都会向该表提交分数。 Don't know what will happen if many users submit at the same time. 不知道如果许多用户同时提交会发生什么。 This table structure should be: gameID, name, score. 该表结构应为:gameID,名称,分数。 So I just make 1 query to view what game's highscore in 1 php page. 因此,我只进行1个查询即可在1个php页面中查看哪个游戏的最高分。

If use multiple tables (each game has its own table), I would have many more tables but much lesser rows(data). 如果使用多个表(每个游戏都有自己的表),我将拥有更多的表,但行(数据)更少。

Which one is better in long run? 从长远来看哪个更好? How about the sql performance? sql性能如何?

I am using php and mysql. 我正在使用php和mysql。

I would make a table for players, one for games and a many-to-many table for highscores with player_id, game_id and the highscore value. 我会为玩家创建一张表,为游戏创建一个表,并为具有player_id,game_id和highscore值的高得分创建一个多对多表。

Discussion 讨论

If you have 100.000 players and 500 games you can get to have a maximum of 50.000.000 entries. 如果您有100.000名玩家和500场比赛,您最多可以拥有50.000.000项。 But they are numerical and very short. 但是它们是数字并且非常短。 It will be a good idea NOT to serve this highscores using live queries but results from cached files. 最好不要使用实时查询来获得这些高分,而是缓存文件的结果。 This is how almost everybody is doing. 几乎每个人都是这样。 You update the cached lists of highscores each day. 您每天更新缓存的高分列表。 This way you do the writes live and the reads from cache and they don´t interfere. 这样,您就可以实时进行写操作以及从缓存中进行读操作,而不会干扰它们。

Another suggestion it will be to reset the highscore table each month, or to keep older data separate based on access frequency. 另一个建议是每月重新设置高分表,或根据访问频率将较旧的数据分开。 Facebook is doing something like this, as I´ve seen in a presentation of their lead developer. 正如我在其主要开发人员的演示中所看到的那样,Facebook正在这样做。

You should go with the single table option. 您应该使用单表选项。 That way when you add more games you don't need to change your database schema. 这样,当您添加更多游戏时,无需更改数据库架构。

Databases are designed to cope with data like this. 数据库旨在处理此类数据。 Having one table with a lot of data will not cause you any problems as long as it is indexed correctly. 只要一个表具有正确的索引,拥有一个包含大量数据的表就不会给您带来任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM