需要数据库表设计方面的帮助
我想在数据库中存储用户最近 5 天的统计信息。我心中有两种类型的数据库表设计。
设计1:
id user_name stats
1 Peter 54 //Day 1 stats for Peter
2 Peter 50 //Day 2 stats for Peter
3 Abc 10 //Day 1 stats for Abc
4 Peter 55 //Day 3 stats for Peter
5 Abc 14 //Day 2 stats for Abc.
设计2:
id user_name day1 day2 day3 day4 day5
1 Peter 54 50 55 - -
2 Abc 10 14 - - -
以上两者哪个更好? 由于我想要仅过去 5 天的统计信息,这就是为什么我想在添加新的一天时覆盖最旧的一天统计信息,以便在任何时候都为用户提供最多 5 天的统计信息。 请告诉我应该如何做。
I want to store last 5 days stats for a user in database. I have two types of db table design in my mind.
Design 1:
id user_name stats
1 Peter 54 //Day 1 stats for Peter
2 Peter 50 //Day 2 stats for Peter
3 Abc 10 //Day 1 stats for Abc
4 Peter 55 //Day 3 stats for Peter
5 Abc 14 //Day 2 stats for Abc.
Design 2:
id user_name day1 day2 day3 day4 day5
1 Peter 54 50 55 - -
2 Abc 10 14 - - -
Which of the above two is better ?
Since I want stats for only last 5 days, thats why I want to overwrite the oldest day stats when adding new day so that at any time there are maximum 5 days stats for the user.
Please tell me how that should be done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
设计1要优越得多。如果您决定将来要记录更多天,您就不需要编写另一组查询来获取这些数据。我猜你的意思是在设计 1 中有一个“日”栏?为什么不使用 DateTime 字段来指示日期,如下所示:
然后您可以在方便时清除数据。 id 主键字段只是为了在查询/更新时简化行标识。
Design 1 is far superior. If you decide you want to record more days in future, you don't want to have to write another set of queries to get to this data. I presume you meant to have a 'day' column in Design 1? Why not have a DateTime field to indicate the day like so:
Then you can purge the data at your own convenience. The id primary key field is simply to simplify row identification when used in queries / updates.
我更喜欢这个:
id UserID value day
Table Users
如果你想要的话要添加或删除更多天,您不必更改表定义。
为了提高速度,您可以通过 UserId 创建和索引
I like this more:
id UserID value day
Table Users
In case you want to add or remove more days you don't have to change the table definition.
For speed you can create and index by UserId
设计 1 更好,但还不够接近。
在设计 1 中,您可以像在设计 2 中一样执行查询
,任何涉及聚合数据、查找最小值或排序的查询充其量都将非常麻烦。
另外,有一天您可能决定存储过去 7 天而不是 5 天的数据,而使用设计 2,您将必须更改数据库定义。
Design 1 is better, and it's not close.
In Design 1, you can do queries like
In Design 2, any query that involves aggregating data, finding the minimum or sorting will be very cumbersome at best.
Plus, one day you might decide to store data from the last 7 days instead of 5, and with Design 2, you'll have to alter the database definition.
我认为你应该再制作一张表,“统计”表。
我心里有这样的想法:
tbl_users
id user_name
tbl_stats
,然后调整您的 SQL 查询以仅检索最后 5 条记录。
I think you should just make 1 more table, "stats" table.
I have something like that in my mind :
tbl_users
id user_name
tbl_stats
and then just adapt your SQL query to retrieve only the last 5 records.
第二个表设计工作效率高于第一个,因为在第二个设计中它很容易管理。天和用户的时间,而且插入一些记录后第一个设计看起来也很复杂......
second on table design work efficiently rather than first one because in second design its is easily to manage no. of days and user, and also first design is also looks like complex after some record inserted...