使用计数或有一个字段,提高? [英] Use count or have a field that tallies?

查看:146
本文介绍了使用计数或有一个字段,提高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当简单的概念,做一个非常基本的留言板系统,我希望用户有一个帖子数。现在我辩论是否在他们的行中有一个提示,每当他们创建一个帖子时添加,或每当他们的帖子被删除时减去一个。然而,我确信,当请求后计数时执行计数查询将更准确,因为不可预见的情况(比如一个线程被删除,它不会正确地降低其计数),但是这似乎会降低效率运行查询每次他们的帖子计数被加载,特别是在他们在同一页面上有10个帖子的情况下,它列出了每个帖子的帖子数。

Fairly simple concept, making an extremely basic message board system and I want users to have a post count. Now I was debating on whether or not to have a tally in their row that is added each time a post by them is created, or subtracted by one each time a post of theirs is deleted. However I'm sure that performing a count query when the post count is requested would be more accurate due to unforseen circumstances (say a thread gets deleted and it doesn't lower their tally properly), however this seems like it would be less efficient to run a query EVERY time their post count is loaded, especially in the case of them having 10 posts on the same page and it lists their post count each post.

/ Advice?

感谢

推荐答案

。除非你的负载是天文数字,否则COUNT不应该是一个问题,并且减少了保存和更新数据的工作量。

Just go for count each time. Unless your load is going to be astronomical, COUNT shouldn't be a problem, and reduces the amount of effort involved in saving and updating data.

只要确保你您的user_id列上的索引,以便您可以有效地使用WHERE子句过滤数据。

Just make sure you put an index on your user_id column, so that you can filter the data with a WHERE clause efficiently.

如果您遇到这样的情况, ,你可以实现缓存策略,但考虑到它是一个简单的留言板,你不应该遇到一段时间。

If you get to the point where this doesn't do it for you, you can implement caching strategies, but given that it's a simple message board, you shouldn't encounter that problem for a while.

刚才看到你对同一个查询在页面上重复10次的第二个问题。不要这样做:)只需拉一次数据并将其存储在变量中。无需多次重复相同的查询。

Just saw your second concern about the same query repeating 10 times on a page. Don't do that :) Just pull the data once and store it in a variable. No need to repeat the same query multiple times.

这篇关于使用计数或有一个字段,提高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆