需要数据库表设计方面的帮助

发布于 2024-10-28 12:26:45 字数 752 浏览 2 评论 0原文

我想在数据库中存储用户最近 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 技术交流群。

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

发布评论

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

评论(5

怀念你的温柔 2024-11-04 12:26:45

设计1要优越得多。如果您决定将来要记录更多天,您就不需要编写另一组查询来获取这些数据。我猜你的意思是在设计 1 中有一个“日”栏?为什么不使用 DateTime 字段来指示日期,如下所示:

id    user_name      day           stats
1     Peter          31-Mar-2011   54
2     Peter          01-Apr-2011   24
3     Abc            01-Apr-2011   23

然后您可以在方便时清除数据。 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:

id    user_name      day           stats
1     Peter          31-Mar-2011   54
2     Peter          01-Apr-2011   24
3     Abc            01-Apr-2011   23

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.

爱格式化 2024-11-04 12:26:45

我更喜欢这个:

id UserID value day

1        0       54     1        //Day 1 stats for Peter
2        0      50     2        //Day 2 stats for Peter

Table Users

id Name
0  Peter

如果你想要的话要添加或删除更多天,您不必更改表定义。

为了提高速度,您可以通过 UserId 创建和索引

I like this more:

id UserID value day

1        0       54     1        //Day 1 stats for Peter
2        0      50     2        //Day 2 stats for Peter

Table Users

id Name
0  Peter

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

吃不饱 2024-11-04 12:26:45

设计 1 更好,但还不够接近。

在设计 1 中,您可以像在设计 2 中一样执行查询

select user_name, avg(stats) as average from user group by user_name order by avg(stats)

,任何涉及聚合数据、查找最小值或排序的查询充其量都将非常麻烦。

另外,有一天您可能决定存储过去 7 天而不是 5 天的数据,而使用设计 2,您将必须更改数据库定义。

Design 1 is better, and it's not close.

In Design 1, you can do queries like

select user_name, avg(stats) as average from user group by user_name order by avg(stats)

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.

贱贱哒 2024-11-04 12:26:45

我认为你应该再制作一张表,“统计”表。

我心里有这样的想法:

tbl_users

id user_name

1    Peter
2     Abc 

tbl_stats

id  num_tbl_users_id   value
1    1                 50
2    1                 53
3    1                 54
4    2                 51

,然后调整您的 SQL 查询以仅检索最后 5 条记录。

SELECT tbl_users.user_name, tbl_stats.value 
FROM tbl_users 
INNER JOIN tbl_stats ON tbl_users.id = tbl_stats.num_tbl_users_id 
WHERE tbl_users.id = 1 
ORDER BY tbl_stats.id DESC LIMIT 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

1    Peter
2     Abc 

tbl_stats

id  num_tbl_users_id   value
1    1                 50
2    1                 53
3    1                 54
4    2                 51

and then just adapt your SQL query to retrieve only the last 5 records.

SELECT tbl_users.user_name, tbl_stats.value 
FROM tbl_users 
INNER JOIN tbl_stats ON tbl_users.id = tbl_stats.num_tbl_users_id 
WHERE tbl_users.id = 1 
ORDER BY tbl_stats.id DESC LIMIT 5;
游魂 2024-11-04 12:26:45

第二个表设计工作效率高于第一个,因为在第二个设计中它很容易管理。天和用户的时间,而且插入一些记录后第一个设计看起来也很复杂......

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...

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