用于向多个用户发送消息的数据库架构

发布于 2024-10-04 12:02:11 字数 900 浏览 0 评论 0原文

我正在寻找最好的解决方案来实现向系统内多个用户发送消息(Facebook 风格)。

我提出了以下想法:其中每个消息都属于 Message_Chain,并且在 Message_status 表中列出了用户发送者和用户接收者。然而,当系统中有数百万条消息时,我担心这种模式的使用效率不是很高。

有人可以建议任何其他解决方案来解决当前的问题吗?或者解释一下为什么我的解决方案会很好?

CREATE  TABLE `message` (
  `msg_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `msg_text` TEXT NOT NULL ,
  `msg_date` DATETIME NOT NULL ,
  PRIMARY KEY (`msg_id`) );

CREATE  TABLE `message_chain` (
  `msgc_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `msgc_topic` VARCHAR(255) NULL ,
  PRIMARY KEY (`msgc_id`) );

CREATE  TABLE `message_status` (
  `msgsta_msg_id` BIGINT UNSIGNED NOT NULL ,
  `msgsta_usr_id` INT UNSIGNED NOT NULL ,
  `msgsta_msgc_id` INT UNSIGNED NOT NULL ,
  `msgsta_is_sender` TINYINT(1)  NULL ,
  `msgsta_is_read` TINYINT(1)  NULL DEFAULT NULL ,
  `msgsta_is_deleted` TINYINT(1)  NULL ,
  PRIMARY KEY (`msgsta_msg_id`, `msgsta_usr_id`);

I'm looking for the best solution to implement messaging to multiple users within the system(Facebook-style).

I'm came up with the following idea: where each Message belongs to the Message_Chain and in the Message_status table user-sender and users-receivers are listed. However I'm afraid that this schema is not very efficient to use when there are millions of messages in the system.

Could anyone suggest any other solution to the current problem? Or explain why my solution will be fine?

CREATE  TABLE `message` (
  `msg_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `msg_text` TEXT NOT NULL ,
  `msg_date` DATETIME NOT NULL ,
  PRIMARY KEY (`msg_id`) );

CREATE  TABLE `message_chain` (
  `msgc_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `msgc_topic` VARCHAR(255) NULL ,
  PRIMARY KEY (`msgc_id`) );

CREATE  TABLE `message_status` (
  `msgsta_msg_id` BIGINT UNSIGNED NOT NULL ,
  `msgsta_usr_id` INT UNSIGNED NOT NULL ,
  `msgsta_msgc_id` INT UNSIGNED NOT NULL ,
  `msgsta_is_sender` TINYINT(1)  NULL ,
  `msgsta_is_read` TINYINT(1)  NULL DEFAULT NULL ,
  `msgsta_is_deleted` TINYINT(1)  NULL ,
  PRIMARY KEY (`msgsta_msg_id`, `msgsta_usr_id`);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑忘罢 2024-10-11 12:02:11

只要同一消息的收件人不多,该架构就应该可以正常工作。我不明白如何让它变得更小或更高效。

我能看到的唯一性能问题是,如果您想要进行广播,即将相同的消息发送给一大群人,或者说系统上的每个用户。发送这样的消息会非常慢(去过那里,做过那件事)。在这种情况下,我会惰性地跟踪此类全局消息的状态,即仅在单个用户打开消息后才为他创建状态行。但如果你没有计划这样的功能,我建议暂时忽略这个问题。

The schema should work just fine as long as there aren't too many recipients of the same message. I don't see how you could make it much smaller or more efficient.

The only performance problem I can see is that if you want to do broadcasting, that is, send the same message to a large group or, say, every user on the system. Sending such a message will be very slow (been there, done that). In that case, I would track the status of such global messages lazily, that is, create the status row for an individual user only after he has opened the message. But if you don't have such a feature planned, I'd say ignore this problem for now.

你是我的挚爱i 2024-10-11 12:02:11

我的解决方案是通过使用更多编程来确定哪些消息发送给谁来避免大量数据存储。

例如,如果您想向系统的所有用户发送消息,可以在 usr_id 列中放置“”,然后以编程方式获取 usr_id = current_usr_id OR '' 的所有消息。然后,您可以执行各种过滤器,并提出自己的语法来创建 messager_user 列表,而无需数据库存储。

似乎是处理/存储之间的权衡......

My solution is to avoid the massive data storage by using more programming to determine which message goes to whom.

For example if you want to send a message to all users of the system, you place a "" in the usr_id column, and then programmmatically you could fetch all messages where the usr_id = current_usr_id OR ''. Then you could do a variety of filters and come up with your own syntax for creating messager_user lists without database storage.

Seems like a trade-off between processing/storage ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文