如何从数据库中预先计算评分系统?

发布于 2024-10-24 20:11:31 字数 143 浏览 2 评论 0原文

Stackoverflow 在您的用户名旁边显示您的声誉。我想它不会访问数据库来计算每个页面渲染的分数(或者可能会?)。当有人给我投票时,用户表中是否会有一个总分字段会更新?

此类系统的最佳实践是什么?如果这是一个显而易见的问题,我很抱歉,我是数据库新手。

Stackoverflow displays your reputation next to your username. I suppose it doesn't hit the database to calculate your score on every page render (or perhaps it does?). Would there be a total score field in the users table that gets updated when someone upvotes me?

What is the best practice for these kind of systems? I'm sorry if it's an obvious question, I'm new to databases.

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

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

发布评论

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

评论(2

安稳善良 2024-10-31 20:11:31

当您登录时,系统会从数据库中提取该号码以及您的所有用户信息。

它可以是一个用户对象。该用户对象有一个属性分数,当您登录时,该属性分数被初始化为数据库中的任何数字。

当您浏览网站并执行操作时,属性会更新,但数据库不会更新。

然后,您只能在对象用户被破坏(通过注销或关闭浏览器)或经过一定时间后更新数据库一次。

When you log in, the number is pulled from the database along with all your users information.

It can be a user object. That user object has a property score which is initialized to whatever that number is in the database when you just log in.

As you go around the website and do stuff, the property is updated but not the database.

You can then update the database only once when the object user is destructed (either by logout or closing the browser) or after a certain amount of time.

睫毛溺水了 2024-10-31 20:11:31

我认为对于这样一个普遍问题的一般答案是:缓存!缓存的想法可能很简单,但正确执行并不总是那么容易。我认为 SO 不仅存储您的总分。它存储所有增加您分数的“事件”。您的总分是根据这些“事件”计算出来的。所以总分已经是某种预先计算或缓存的值。如果您不想为每个请求访问数据库,可以将值缓存在内存中。 Memcached 通常用于此类工作,但当然还有其他选择。

I think the general answer for such a general question is: Caching! The idea of caching might be simple, but it's not always easy to do it right. I would assume that SO not only stores your total score. It stores all "events" which add to your score. Your total score is calculated out of these "events". So to the total score is already some kind of precalculated or cached value. If you don't want to hit the database for each request, you can cache the values in memory. Memcached is used often for such jobs, but there are other options of course.

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