数据库优化 - 有总计字段或查询表吗?

发布于 2024-10-09 07:12:36 字数 393 浏览 4 评论 0原文

我有一个应用程序,用户可以根据他们执行的操作获得积分 - 简单的操作可以获得 1 分,困难的操作可以获得 2 分。 我希望向用户显示他在我的应用程序中获得的积分总数以及本周(从星期一午夜开始)获得的积分。

我有一个表记录所有动作,以及它们的时间和点数。

我有两种选择,但不确定哪个更好:

  1. 每次用户看到报告时,都会执行查询并对用户获得的积分进行求和
  2. 向每个用户添加两个字段,记录迄今为止获得的积分数(总计和每周) )。每周积分值每周一午夜将设置为 0。

第一个选项更容易,但恐怕因为我会得到很多用户和操作查询将需要很长时间。

第二个选项存在操作表与汇总值不一致的风险。

我对您认为最好的选择非常感兴趣。

谢谢, 多里安

I have an app where users get points for actions they perform - either 1 point for an easy action or 2 for a difficult one.
I wish to display to the user the total number of points he got in my app and the points obtained this week (since Monday at midnight).

I have a table that records all actions, along with their time and number of points.

I have two alternatives and I'm not sure which is better:

  1. Every time the user sees the report perform a query and sum the points the user got
  2. Add two fields to each user that records the number of points obtained so far (total and weekly). The weekly points value will be set to 0 every Monday at midnight.

The first option is easier, but I'm afraid that as I'll get many users and actions queries will take a long time.

The second option bares the risk of inconsistency between the table of actions and the summary values.

I'm very interested in what you think is the best alternative here.

Thanks,
Dorian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱冒险 2024-10-16 07:12:36

选择选项一。 稍后扩展

我的想法是采用更简单的方法,每次查询表,当您的网站上有太多用户导致服务器速度减慢时,您可以在那时进行切换。如果您的用户太多,导致服务器速度变慢,那么您可能还需要扩展应用程序的其他部分,但不要在早期阶段过度扩展应用程序。当需要扩展时进行扩展。

我的观点取自 37signals 的书“Getting Real”,第 4 章 - Scale Later
http://gettingreal.37signals.com/ch04_Scale_Later.php

Take option one. Scale later

My thought is to take the simpler approach of querying the table each time and when you have the problem of so many users on your site that it's slowing down your server, you can switch at that time. If you have so many users that your server is slowing down then probably you'll need to scale other parts of your application too, but don't go out of your way terribly at these early stages to scale your application. Scale when you need to scale.

My point of view is taken from the 37signals book "Getting Real", chapter 4 - Scale Later
http://gettingreal.37signals.com/ch04_Scale_Later.php

放飞的风筝 2024-10-16 07:12:36

两种解决方案都同样有效。你自己说得对:第一个替代方案会更容易实现一些,但需要在用户每次进入分数页面时总结分数。另一方面,对于第二种选择,您只需要在用户表中添加和维护 2 个附加属性。如果您仔细编程,这几乎不会危及应用程序的完整性。

Both solutions will work equally well. You put it right yourself: The first alternative will be a little easier to implement, but will need to sum up scores every time a user enters the score page. On the other hand for the second alternative, you only need to add and maintain 2 additional attributes in the user table. This will hardly put the integrity of your application into jeopardy if you program carefully.

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