在PHP/MySQL中实现递归注释 [英] Implementing Recursive Comments in PHP/MySQL

查看:86
本文介绍了在PHP/MySQL中实现递归注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个评论系统,人们可以在其中评论其他评论,这些评论在页面上显示为递归线程. ( Reddit的评论系统是我正在尝试的示例实现),但是我对如何实现这样一个不会很慢且计算量很大的系统感到困惑.

I'm trying to write a commenting system, where people can comment on other comments, and these are displayed as recursive threads on the page. (Reddit's Commenting system is an example of what I'm trying to achieve), however I am confused on how to implement such a system that would not be very slow and computationally expensive.

我想象每个评论都将存储在评论表中,并包含一个parent_id,这将是另一个评论的外键.我的问题在于如何在不进行大量查询的情况下获取所有这些数据,然后如何有效地将注释组织到订单所属的组织中.有人对如何最好地实现这一点有任何想法吗?

I imagine that each comment would be stored in a comment table, and contain a parent_id, which would be a foreign key to another comment. My problem comes with how to get all of this data without a ton of queries, and then how to efficiently organize the comments into the order belong in. Does anyone have any ideas on how to best implement this?

推荐答案

尝试使用嵌套集合模型.在在MySQL中管理分层数据中进行了描述.

Try using a nested set model. It is described in Managing Hierarchical Data in MySQL.

最大的好处是您不必使用递归来检索子节点,并且查询非常简单.缺点是插入和删除需要更多的工作.

The big benefit is that you don't have to use recursion to retrieve child nodes, and the queries are pretty straightforward. The downside is that inserting and deleting takes a little more work.

它的缩放比例也非常好.我知道一个极其庞大的系统,该系统使用此方法存储讨论层次结构.

It also scales really well. I know of one extremely huge system which stores discussion hierarchies using this method.

这篇关于在PHP/MySQL中实现递归注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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