简体   繁体   English

通知和PHP + MySQL设计

[英]Notification and PHP+MySQL design

I'm making a website that have posts and replies system. 我正在制作一个具有帖子和回复系统的网站。

I'd like to do is when someone replies, sending notification to those who have ever replied (or involved) the post. 我想做的是,当有人回复时,向曾经回复(或参与)帖子的人发送通知。

My thought is to create a table named Notification , contains message and seen (seen/unread) field. 我的想法是创建一个名为Notification的表,其中包含message和已seen (已看到/未读)字段。 Once people replied, INSERT record to the Notification table. 人们回复后,INSERT记录到Notification表中。

It's seems easy and intuitive, but if there are lots of people involved in, for example, the 31st user replies, 30 people who have ever replied will receive notification. 这似乎很容易而且很直观,但是例如,如果涉及到很多人,例如第31位用户的回复,那么曾经回答过的30个人将收到通知。 This will make 30 rows of SQL records. 这将产生30行SQL记录。 And the 32nd user will make 31 records. 而第32位用户将记录31条记录。 Then total number of rows will become 30+31=61 . 然后,总行数将变为30+31=61

My question is 我的问题是

  1. Is that a good way to handle notification system? 这是处理通知系统的好方法吗?
  2. If so, how to deal with the duplicate notification (haven't seen but has new reply) 如果是这样,如何处理重复的通知(尚未看到但有新回复)
  3. As above, will this make a huge server load? 如上所述,这会增加服务器负载吗?

Thank you so much. 非常感谢。

I was creating similar system. 我正在创建类似的系统。 Here is my experience: 这是我的经验:

  1. My notification table looks like: id (int) | 我的通知表如下所示:id(int)| user_id (int) | user_id(int)| post_id (int) | post_id(int)| last_visited (datetime). last_visited(日期时间)。

  2. user_id + post_id is an unique composite index . user_id + post_id是唯一的 复合索引

  3. So when a user opens the page, I'm looking for an entry (user_id + post_id) in the database. 因此,当用户打开页面时,我正在数据库中寻找条目(user_id + post_id)。 If I find it, then I update the last_visited field if I don't find, then create new row. 如果找到它,那么如果找不到,我将更新last_visited字段,然后创建新行。

  4. When I need list messages for notification I'm just query all messages that was created after last_visited time. 当我需要列表消息进行通知时,我只查询在last_visited时间之后创建的所有消息。

  5. Also I have cron sript that clean notification for closed posts or banned users. 我也有cron sript为关闭的帖子或被禁止的用户提供干净的通知。

As for your questions: 至于您的问题:

1 and 2: You have to find a balance between the amount of data that will be stored and site performance. 1和2:您必须在要存储的数据量和站点性能之间找到平衡。 If you don't need to store all this data you can follow my way. 如果您不需要存储所有这些数据,可以按照我的方式进行。 If this data is needed your way is better. 如果需要此数据,则更好。

3: It depends on the number of visitors and other functionality. 3:这取决于访问者的数量和其他功能。 But here is some advices. 但是这里有一些建议。 You must use indexes for MySql table for better perfomance. 您必须为MySql表使用索引以获得更好的性能。 Also you should think about cron script that will remove useless notifications. 另外,您还应该考虑将删除无用的通知的cron脚本。 If you have huge amount of visitors more than 700k per day you shoulf think about MogoDb or other high perfomance noSql database. 如果每天有大量访问者超过70万,则应该考虑MogoDb或其他高性能noSql数据库。

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

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