简体   繁体   English

php每个主题最后只有100条评论

[英]php every topic last only 100 comments

i dealing with a case that i allow every topic to have only 100 lasts comments 我处理的情况是,我允许每个主题只有100条持续评论

in case a topic have already 100 comments , and a new comment i coming i want to delete the very first comment in the 100 comments chain and add the new one , here is an example : 如果一个话题已经有100条评论,而我要发表一个新评论,我想删除100条评论链中的第一个评论并添加新评论,下面是一个示例:

1,2,3 .... 99 , 100 1,2,3 .... 99,100

2,3,4 ....100 , 101 2,3,4 .... 100,101

as you can see the very first comment that was far behind the others got deleted and the new one got in the 100 comments chain 如您所见,第一个评论远远落后于其他评论,而新评论进入了100条评论链

here starts the problem , if i have in my case forum , and this forum have hundreds thousands of topics , it can reach millions of comments , which means if i check how many comments there are with every new incoming comment it will cause slowing the site with every comment adding , how can i minimize the data base query's ? 这开始了问题,如果我的案例论坛中有这个主题,并且这个论坛有成千上万的主题,它可以到达数以百万计的评论,这意味着如果我检查每个新收到的评论中有多少评论,它将导致网站速度变慢每添加一条注释,我如何才能最小化数据库查询? is there any system / known ways to facing that kind of things? 是否有任何系统/已知方式来面对这种事情?

Why would you delete old comments? 为什么要删除旧评论? If you want to show last 100 comments, then just SELECT id, thread_id, user_id, comment_body from COMMENTS where thread_id = @thread_id LIMIT 100 . 如果要显示最后100条注释,则只需SELECT id, thread_id, user_id, comment_body from COMMENTS where thread_id = @thread_id LIMIT 100 Also be sure to have indexing on foreign column so that it gets queried fast and query only columns you need. 另外,请确保对外部列进行索引,以便快速查询它并仅查询所需的列。

And no, if you are going to be wise with queries, apply indexing where needed then you don't need to worry about each comment slowing down the database. 不,如果您要明智地使用查询,请在需要的地方应用索引,然后您就不必担心每个注释都会降低数据库的速度。 Will you have millions of millions of comments? 您将有数亿条评论吗? If so, you can think about partitioning the database say every 1000000 * threads based on thread_id. 如果是这样,您可以考虑基于thread_id对数据库进行分区,例如每1000000 *个线程。

You may be interested in this question: Database architecture for millions of new rows per day 您可能对此问题感兴趣: 每天有数百万个新行的数据库体系结构


* For anyone who reads: Don't assume this number as some advice or suggestion out of experience. *对于阅读内容的任何人:不要以经验为依据不以这个数字为参考。 Never say: "someone on SO mentioned this number x, so..." I have no experience or benchmarks to say that it would be good to do it at this number. 永远不要说:“ SO上有人提到了这个数字x,所以...”我没有经验或基准可以说用这个数字来做会很好。 I'm only creating my first partition myself. 我只是自己创建第一个分区。 Evaluate yourself what is good for you. 评估自己对自己有什么好处。

What's your database structure for this stuff? 这东西的数据库结构是什么? Presumably you have a "posts" table, and a "comments" table, which links back to the posts table. 大概您有一个“ posts”表和一个“ comments”表,这些表链接回到posts表。 Assuming decent design, the comments will have an auto_increment primary key, so your logic would be: 假设设计得体,注释将具有一个auto_increment主键,因此您的逻辑将是:

1. insert new comment
2. count the comments attached to the post. If it's > 100, then
3. find min(comments.id) and delete that ID.

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

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