将聊天消息存储在数据库中的最佳方法? [英] Best way to store chat messages in a database?

查看:552
本文介绍了将聊天消息存储在数据库中的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个聊天应用程序,我希望获得聊天对话中发送的所有消息的完整历史记录.目前,我将每个消息作为一行存储在称为消息"的表中.我知道这个表可能会变得很大,因为像"Hi"这样的小消息也会有自己的数据库记录.

I'm building a chat app and I want a full history off all messages ever sent in the chat conversation. At the moment I am storing each message as a single row in a table called 'messages'. I am aware that this table could grow huge as even small messages like 'Hi' would have their own database record.

谁能推荐一个更具扩展性的mysql解决方案?我不要求个别邮件是可搜索,可编辑或可删除的.整个对话可以存储在一个巨大的字段中吗?

Can anyone recommend a more scalable mysql solution? I don't require the individual messages to be searchable, editable or deletable. Could the whole conversation be stored in one huge field?

很想听听您的想法!

推荐答案

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

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

实际上,您可以在Stack Overflow中找到指向聊天示例架构的链接:示例

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

如果您仍然担心大小,可以对消息分组进行一些优化,例如向应用程序中添加缓冲区,该缓冲区仅在一段时间(例如1分钟左右)后才推送;这样一来,您将避免只收到1条线路消息

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

这篇关于将聊天消息存储在数据库中的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆