将不同聊天的消息存储在单个数据库表中 [英] Storing messages of different chats in a single database table

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

问题描述

我正在一个聊天网站上。如何存储2个不同的聊天消息。我是否必须为每个聊天创建一个新表,还是可以有一个存储所有聊天的表?

I am working on a chatting website. How can I store messages of 2 different chats. Do I have to create a new table for each chat, or can I have a single table storing all the chats?

从长远来看,以后的方法是否会受到影响(例如在搜索过程中),因为每次用户打开聊天时都会从该表中检索所有消息?

Would the later approach be affected in the long run (i.e. during searching), as all the messages will be retrieved from this table every time a user opens his chat?

推荐答案

我建议您只使用一个表来存储消息,您将需要几个表来维护其他相关数据。
也将一对一聊天也视为群聊,唯一的区别是对于最终用户,它只被视为1-1。

Here is what I would recommend, use only one table for storing messages, you will need few more tables for maintaining other related data. Also treat one to one chat also as group chat only difference is for end user it is viewed as 1-1 only.

下面只是基本结构要开始使用,实际上,您将必须添加更多列或更改结构以支持数据同步,读取,传递收件人,附件等

Below is just the basic structure to get you started, in actual you will have to add more columns or change the structure to support data syncing, read, delivered recipients, attachments, etc

Table: User
Columns: userId, name, image and other user info columns

Table: Group
Columns: groupId, name

Table: Group_User_X
Columns: groupId, userId

Table: Message
Columns: messageId, senderUserId, groupId, content

现在,要为任何给定用户加载消息,您只需将Group_User_X和Message表联接起来,并获取该用户所属组的消息。

Now, to load messages for any given user, you can simply join Group_User_X and Message table and fetch messages for the groups in which the user belong.

如果您需要其他帮助,可以通过contact@applozic.com与我联系

If you need any further help you can reach out to me at contact@applozic.com

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

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