使用 php 统计帖子或存储在数据库中 [英] Count posts with php or store in database

查看:46
本文介绍了使用 php 统计帖子或存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些表:

  • id
  • 姓名
  • 说明
  • id
  • 论坛ID
  • 身体

我想知道我应该在论坛中创建一个字段来存储在该论坛中发表的帖子数量吗?每次有人发帖时更新它.还是我应该用 php 计算它们?这是一个巨大的性能差异吗?

What I wonder is should I create a field in forum where i store the number of posts that been made in that forum? Update it every time someone makes a post. or should I count them with php? Is it a huge performance difference?

推荐答案

你应该只查询你的帖子表并用你想要的 forumID 计算帖子.

You should just query your posts table and count the posts with the forumID you want.

SELECT count(*) FROM `posts` WHERE `forumID` = XX;

无论如何,您应该在 forumID 上有一个索引,因为您会经常在查询中使用它.使用该索引,此查询将运行得非常非常快.

You should have an index on forumID anyway, since you will use it often in your queries. With that index, this query will run very, very quickly.

这篇关于使用 php 统计帖子或存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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