针对线索评论的可扩展解决方案
我不知道如何在 PHP 和 MySQL 中创建一个可以一次处理数百条评论的线程评论系统。
像这样的东西是我唯一能想到的
$query = execute_query...('SELECT * FROM `comments` WHERE `post` = "1"');
foreach($query as $comment){
$replies = execute_query...('SELECT * FROM `comment_replies` WHERE `comment` = "' . $comment['id'] . '"');
if($replies){
echo $comment['body']; //....
foreach($replies as $reply){ /*....*/ }
}
else{
echo $comment['body'];
}
}
所以我需要有关数据库结构的提示以及如何在考虑性能的情况下检索所有线程评论:)
I'm not sure how to create a threaded comments system in PHP and MySQL which can handle hundreds of comments at a time.
Something like this is the only thing I can come up with
$query = execute_query...('SELECT * FROM `comments` WHERE `post` = "1"');
foreach($query as $comment){
$replies = execute_query...('SELECT * FROM `comment_replies` WHERE `comment` = "' . $comment['id'] . '"');
if($replies){
echo $comment['body']; //....
foreach($replies as $reply){ /*....*/ }
}
else{
echo $comment['body'];
}
}
So I need tips on database structure and how I can retrive the all the threaded comments with performance in mind please :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您会找到这篇文章管理 MySQL 中的分层数据 有帮助。这两个表可以轻松合并为一个
I'm sure you'll find the article Managing Hierarchical Data in MySQL helpful. Those two tables can be easily merged into one
为什么不加入 comments 和 comment_replies 表呢?
然后只需添加一个额外的生成字段来显示它是评论还是comment_reply。并在 foreach 中使用 if 来选择它们,例如:
同时检查评论 ID 是否发生变化,以便将它们分开。
Why not join the comments and the comment_replies table?
then just add a extra generated field that shows if its a comment or a comment_reply. And select them with a if in a foreach like:
Also check if the comment id changes so you can seperate them.
我会选择邻接列表模型而不是嵌套集模型,因为使用嵌套集模型,我必须在表上完成的每个 INSERT 和 DELETE 操作上构建整个树结构,如果我有 1000 条记录,对于单个 INSERT 或DELETE 操作必须更新所有记录,需要更多时间来执行。
我的数据库结构是:
我还用 php mysql jquery 和 ajax 编写了一个小型线程评论系统。看看 http://pradipchitrakar.com.np/programming/threaded-comment -系统/
I would choose Adjacency List Model over Nested Set Model because with Nested Set Model, I would have to build the whole tree structure on every INSERT and DELETE operation done on the table and if I have 1000′s of records, for a single INSERT or DELETE operation all the records has to be updated taking more time to execute.
My database structure would be:
Also I have written a small threaded comment system with php mysql jquery and ajax. Take a look at http://pradipchitrakar.com.np/programming/threaded-comment-system/