简体   繁体   English

如何设计一个高效的Like系统?

[英]How to design an efficient Like system?

I'm trying to create a Like/Unlike system akin to Facebook's for an existing comments section of a website, and I need help in designing the system. 我正在尝试为网站的现有评论部分创建类似于Facebook的Like / Unlike系统,并且在设计系统时需要帮助。

Currently, every product on the website has a comments section and members can post and like comments. 当前,网站上的每个产品都有评论部分,成员可以发表和发表评论。 I need to know each member has posted how many comments and each of his comments has received how many likes. 我需要知道每个成员发布了多少评论,并且他的每个评论都收到了多少赞。 Of course, I need to know who liked what comments too (partly so that I can prevent a user from liking a comment more than once) for analytical purposes. 当然,出于分析目的,我需要知道谁也喜欢什么评论(部分原因是为了防止用户多次喜欢评论)。

The naive way of implementing a Like system to the current comments module is to create a new table in the database that has foreign keys to the CommentID and UserID. 对当前评论模块实施Like系统的天真的方法是在数据库中创建一个新表,该表具有CommentID和UserID的外键。 Then for every "like" given to a comment by a user, I would insert a row to this new table with the targeting comment ID and user ID. 然后,对于用户给评论的每个“赞”,我都会在此新表中插入一行,其中包含目标评论ID和用户ID。

While this might work, the massive amount of comments and users is going to cause this table to grow quickly and retrieving records from and doing counts on this huge table will become slow and inefficient. 尽管这可能行得通,但大量的注释和用户将导致此表快速增长,并且从此巨大表中检索记录并进行计数将变得缓慢且效率低下。 I can index either one of the columns, but I don't know how effective it would be. 我可以为任一列建立索引,但是我不知道它的有效性。 The website has over a million comments. 该网站有超过一百万条评论。

I'm using PHP and MySQL. 我正在使用PHP和MySQL。 For a system like this with a huge database, how should I designing a Like system so that it is more optimised and stable? 对于具有庞大数据库的类似系统,我应该如何设计“类似”系统,使其更加优化和稳定?

Your main concern will be a lot of counts, so the easy thing to do is to keep a separate count in your comments table. 您主要关心的是很多计数,因此,要做的一件简单的事情就是在注释表中保留一个单独的计数。

Then you can create a TRIGGER that increments/decrements the count based on a like/unlike. 然后,您可以创建一个TRIGGER ,根据喜欢/不喜欢增加/减少计数。

That way you only use the big table to figure out if a user already voted. 这样,您仅使用大表来确定用户是否已投票。

For scalability, do not include the count column in the same table with other things. 对于可扩展性, 包括与其他事物相同的表计列。 This is a rare case where "vertical partitioning" is beneficial. 这是一种罕见的情况,其中“垂直分区”是有益的。 Why? 为什么? The LIKEs/UNLIKEs will come fast and furious. 喜欢/不喜欢的人会来得非常快。 If the code to do the increment/decrement hits a table used for other things (such as the text of the Comment), there will be an unacceptable amount of contention between the two. 如果执行递增/递减的代码命中用于其他用途的表(例如Comment的文本),则两者之间的争用量将不可接受。

This tip is the first of many steps toward being able to scale to Facebook levels. 本技巧是能够扩展到Facebook水平的众多步骤中的第一步。 The other tips will come, not from a free forum, but from the team of smart engineers you will have to hire to get to that level. 其他技巧不是来自免费论坛,而是来自聪明的工程师团队,您必须聘请这些技巧才能达到这一水平。 (Hints: Sharding, Buffering, Showing Estimates, etc.) (提示:分片,缓冲,显示估计等)

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

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