简体   繁体   English

Laravel 5.5-100万关注者的通知/子通知?

[英]Laravel 5.5 - Notifications/Subnotifications for 1 million followers?

I use a notifications table and a subnotifications table, and I am also using queues so it runs in the background when a user posts something. 我使用了一个notifications表和一个subnotifications notifications表,并且我还使用了队列,因此当用户发布内容时它在后台运行。 When a user has 10 followers and they create a post, the notifications table gets a single entry which includes the post data for the notification, and the subnotifications table gets 10 entries (one subnotification per follower, each referring to the id of the notification, so we don't have to repeat the notification data 10 times, with a read_at to know if it was read or not by that follower). 当一个用户有10名追随者和他们建立讯息中, notifications表得到的单个条目,其包括用于所述通知后数据,并且subnotifications表得到10个条目(每跟随一个subnotification,每个参照通知的ID,因此我们不必重复通知数据10次,只需使用read_at即可知道该关注者是否读取了通知数据)。

This is quick and works great without any issues. 这是快速的,并且效果很好,没有任何问题。 However, when testing with 1 million followers, it takes about ~6 hours to insert the subnotifications for one post! 但是,当对一百万个关注者进行测试时,大约需要6个小时才能插入一个帖子的子通知! This of course is not acceptable, as it is takes too long to insert 1 million subnotifications, one per follower. 这当然是不可接受的,因为插入一百万个子通知花费的时间太长,每个追随者一个。 Imagine that same user posts 10 posts, that'll be like ~60 hours of inserting and 10 million subnotification rows. 想象一下,同一位用户发布了10条帖子,就像大约60小时的插入时间和1000万个子通知行。

I just want followers to know there is a new post if they didn't read it yet. 我只是想让追随者知道是否有新帖子,如果他们还没有阅读的话。 Is there a better, more efficient way that scales? 有没有更好,更有效的扩展方式?

UPDATE: Stuck with current approach see below... 更新:卡在当前方法见下文...

If a follower $user has 100 leaders they follow (which they followed at different created_at timestamps of course in the followers table), what would the correct query be to know about leader new posts from the time the follower followed each leader? 如果关注者$user有100位领导者(他们遵循的是跟随者表中的时间戳,当然是在不同的created_at时间戳),那么从关注者关注每个领导者起,关于领导者新帖子的正确查询是什么? I get stuck at created_at with this pseudo code: 我被这个伪代码卡在created_at上:

// Assume `leader_id` is a column in the notifications table
DB::table('notifications')
  ->whereIn('leader_id', $leaderIds)
  ->where(`created_at`, '>', $whatTimestampsGoHere)
  ->paginate(20);

There is 100 different timestamps and I am stuck on how to solve this one correctly and efficiently. 有100种不同的时间戳,我坚持如何正确,有效地解决这一问题。 Any ideas? 有任何想法吗?

As stated in the comments, you can reduce the inserts, if you only insert to the child table ie subnotifications when the user reads it and not on creating it on the notification creation, which avoids that issue. 正如评论所说,可以减少插入,如果你只插入到子表即subnotifications当用户读取它,而不是对通知的创建,从而避免这一问题创造它。 When trying to check if user has seen the notification, just check if they exist in subnotifications for the user in question and the notification. 当试图检查用户是否已经看到了通知,只是检查,如果他们在存在subnotifications有问题的用户和通知。

Also as said, when fetching notifications to show to users fetch them from notifications but limit the notifications to the notifications created after the user started following so that new users don't get flooded with notifications. 同样,如前所述,在获取要显示给用户的通知时,将从notifications获取notifications但将通知限制为用户开始关注之后创建的通知,以免新用户被通知所淹没。

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

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