简体   繁体   English

论坛,获取最新帖子/主题

[英]Forum, get last post/topic

Basically, I'm writing a small forum script for an intranet. 基本上,我正在为Intranet编写一个小的论坛脚本。

I've got 3 mysql (MySQLi) tables for the forum: 我为论坛准备了3个mysql(MySQLi)表:

forum_answer - holds replies forum_question - holds first posts forum_categories - holds names and description of categories. forum_answer-保留答复forum_question-保留第一篇文章forum_categories-保留类别的名称和描述。

Ok, so on the forum index, I've got a table to fetch all the categories, however, I've also one column to fetch the last post made in that category/forum. 好的,所以在论坛索引上,我有一张表格来提取所有类别,但是,我也有一列来提取该类别/论坛中的最新帖子。

Do you have any ideas on how I could get the last post made in that forum? 您对我如何获得该论坛的最新帖子有任何想法吗?

I was thinking of maybe adding a new column on the forum_categories table, and updating it each time a post is made, however, that could get messy. 我当时正在考虑在forum_categories表上添加一个新列,并在每次发帖时对其进行更新,但这可能会变得混乱。

Thanks :) 谢谢 :)

Query-wise, you can get it with your query to get the list of categories using something similar to: 在查询方面,您可以使用类似以下内容的查询来获取类别列表:

 select forum_categories.id, forum_categories.name, max( forum_answer.id ) as 
 from forum_categories
 left join forum_questions on forum_questins.category_id = forum_categories.category_id
 left join forum_answers on forum_answers.question_id = forum_questions.question_id
 group by forum_categories.id, forum_categories.name

How expensive an operation it is depends on the nature of your forums. 这项操作的费用取决于您论坛的性质。 Updating a column on the category every time someone posts might more expensive if people are posting frequently. 如果人们经常发布,则每次有人发布时更新类别上的列可能会更昂贵。 If people are loading the list of categories frequently, the joins might be the more expensive operation. 如果人们经常加载类别列表,则连接可能是更昂贵的操作。

You might also find it beneficial to create a view to pull your category list from: 您可能还会发现创建视图从以下类别中提取类别列表很有用:

 create view category_list as
 select forum_categories.id, forum_categories.name, max( forum_answer.id ) as latest_asnwer_id
 from forum_categories
 left join forum_questions on forum_questins.category_id = forum_categories.category_id
 left join forum_answers on forum_answers.question_id = forum_questions.question_id
 group by forum_categories.id, forum_categories.name

Which you could then access as though it were a table. 然后,您可以像访问表一样访问该表。

You are actually thinking in the right direction. 您实际上是在朝着正确的方向思考。

You should definitely "cache" the last post for a question and the last post for a forum and store them in your [forum_question] and [forum_categories] tables respectively. 您绝对应该“缓存”问题的最新帖子和论坛的最新帖子,并将它们分别存储在[forum_question]和[forum_categories]表中。 "Cache" meaning update them each time somebody adds a new post or delete a post. “缓存”表示每次有人添加新帖子或删除帖子时更新它们。

When your forum content amount reaches some threshold, the live calculations of last posts will be terribly slow. 当您的论坛内容量达到某个阈值时,最新帖子的实时计算将非常缓慢。 By updating the "cache" each time a new post is submitted you divide this huge one-time calculation work into small updates distributed onto many requests and thus almost unnoticeable. 通过在每次提交新帖子时更新“缓存”,您可以将大量的一次性计算工作划分为对许多请求进行分配的小更新,因此几乎不会引起注意。

The only hit you get when you delete a post, then you need to update cache for the question and the forum. 删除帖子后,您获得的唯一成功是,您需要更新问题和论坛的缓存。 But this is a rare event, so the price can be afforded. 但这是罕见的事件,因此价格可以承受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM