在数据库中存储聊天消息的最佳方式?

发布于 2024-11-29 08:34:34 字数 1436 浏览 1 评论 0原文

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

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

发布评论

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

评论(3

可爱暴击 2024-12-06 08:34:34

将整个历史记录保存在数据库中没有任何问题,它们已经为此类任务做好了准备。

实际上,您可以在 Stack Overflow 中找到聊天模式示例的链接: 示例

如果如果您仍然担心大小,您可以对组消息应用一些优化,例如向应用程序添加一个缓冲区,仅在一段时间后(例如 1 分钟左右)推送;这样你就可以避免只有 1 行消息

There's nothing wrong with saving the whole history in the database, they are prepared for that kind of tasks.

Actually you can find here in Stack Overflow a link to an example schema for a chat: example

If you are still worried for the size, you could apply some optimizations to group messages, like adding a buffer to your application that you only push after some time (like 1 minute or so); that way you would avoid having only 1 line messages

宫墨修音 2024-12-06 08:34:34

您可以为 x 个对话创建一个数据库,其中包含这些对话的所有消息。这将允许您在每次 x 超出时添加新的数据库(或服务器)。 X 是您的基础设施支持的对话数量(取决于您的硬件,...)。

问题仍然是,同一数据库上可能存在大量对话(包含大量消息)。例如,您有数据库 A 和数据库 B,每个数据库存储 1000 个对话。服务器 A 上的“大”对话可能比服务器 B 上的对话多得多(因为这是用户创建的内容)。您可以添加一个包含查找的“主”数据库,在该数据库/服务器上可以找到单个对话(或者您有一个模式可以从散列/模数或其他内容分配数据库)。

也许您可以找到处理相同问题的现实世界架构(您可能不是第一个),并且已经解决了。

You could create a database for x conversations which contains all messages of these conversations. This would allow you to add a new Database (or server) each time x exceeds. X is the number conversations your infrastructure supports (depending on your hardware,...).

The problem is still, that there may be big conversations (with a lot of messages) on the same database. e.g. you have database A and database B an each stores e.g. 1000 conversations. It my be possible that there are far more "big" conversations on server A than on server B (since this is user created content). You could add a "master" database that contains a lookup, on which database/server the single conversations can be found (or you have a schema to assign a database from hash/modulo or something).

Maybe you can find real world architectures that deal with the same problems (you may not be the first one), and that have already been solved.

泛滥成性 2024-12-06 08:34:34

如果我们假设你也没有读取数据。

在我看来,这听起来像是审计\日志记录要求,
如果是,则不需要数据库来存储聊天消息。

只需将对话附加到文本文件(每天 1 个文件?)。
该文件可能如下所示:

chat-1-bob 201101011029, hi
chat-1-jen 201101011030, how are you?
chat-1-bob 201101011030, fine thanks.    
chat-1-jen 201101011035, have you spoken to bill recently?    
chat-2-bob 201101021200, hi
chat-2-bill 201101021201, Hey Bob,
chat-2-bill 201101021203, what time do you call this?
chat-2-bob 201101021222, about 12:22

我认为您会发现很难获得更简单的可扩展审计解决方案。

如果您的需求发生变化并且需要搜索\编辑\删除,那么数据库会更合适。

If we assume that you do not read the data too.

This sounds to me like an audit\logging requirement,
if it is, you do not need a database to store the chat messages.

Just append the conversation to a text file (1 file per day?).
The file could look like this:

chat-1-bob 201101011029, hi
chat-1-jen 201101011030, how are you?
chat-1-bob 201101011030, fine thanks.    
chat-1-jen 201101011035, have you spoken to bill recently?    
chat-2-bob 201101021200, hi
chat-2-bill 201101021201, Hey Bob,
chat-2-bill 201101021203, what time do you call this?
chat-2-bob 201101021222, about 12:22

I think you will find it hard to get a more simple scaleable audit solution.

If your requirements change and you need to search\edit\delete then a database would be more appropriate.

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