构建 mySQL
我有一张带有联系人的桌子,另一张带有消息的桌子。在消息表中,我有一个单元格,其中保存了消息发送到的那些成员的 ID 序列化字符串。
现在,我将附加一个唯一标识符令牌,当成员响应时,我将能够跟踪它是哪个成员。我是否应该将此信息存储在单独的表中,或者向消息表中添加另一个单元格,该单元格也可以保存一串已回复的成员?
逻辑告诉我,单独的表会更容易,但同时它可以增长得非常快,因为可能有多个消息接收者组,并且每个组可以有多个消息,这将转换为多行,而不是一个带有序列化字符串的单个单元格。
I have a table with contacts and another one with message. In the message table I have a cell where I keep a serialized string of IDs of those members that the message was sent to.
Now, I am going to append a unique identifier token and when a member responds I'll be able to track which member it is. Shall I store this info in a separate table, or add another cell to message tables, which can also hold a string of responded members?
Logic tells me that a separate table would be easier, but at the same time it can grow pretty fast as there could be multiple groups of recipients of messages, and each can have multiple messages, which will translate into multiple rows, as opposed to a single cell with a serialized string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用单独的表很可能会更容易、更快。
例如,如果您想查找发送给特定用户的所有消息,只需一个简单的查询:
WHERErecipient = id
。该查询将执行良好,因为它可以使用索引。如果将其存储为文本字段中逗号分隔的 ID 列表,则必须使用例如
FIND_IN_SET
。此查询将无法使用索引。It will most likely be easier and faster with the separate table.
For example, if you want to find all the messages sent to a specific user it will be a simple query:
WHERE recipient = id
. This query will perform well because it can use an index.If you store it as a comma separated list of IDs in a text field you will have to write a query using for example
FIND_IN_SET
. This query won't be able to use an index.我认为传统的方法是使用连接表。因此,如果您有一个
Contacts
表和一个Messages
表,则可以创建一个ContactMessagesSent
表,该表具有两个的主键联系人
和消息
作为列,用于跟踪哪些消息发送给了哪些联系人。您可以创建另一个联接表ContactMessagesReceived
来跟踪响应。因此,
如果您使用正确的索引,即使表中有很多行,它也会表现良好。
I think the conventional approach would be to have join tables. So if you have a
Contacts
table and aMessages
table, you could create aContactMessagesSent
table, which has the primary keys of bothContacts
andMessages
as columns, to keep track of which messages were sent to which contacts. You could create another join tableContactMessagesReceived
to track responses.So
If you use proper indexing, it will perform well even if there are lots of rows in the table.
创建单独的表。存储序列化的成员字符串首先是一个错误的决定。对这样的字段进行查询既麻烦又缓慢。
创建一个单独的交叉链接表,将成员绑定到消息,以便您可以实际通过它进行查询。然后,您可以向该表添加“已回复”字段来标记成员是否回复。
Create a separate tables. Storing a serialized string of members was a wrong decision in the first place. Querying on such a field is cumbersome and slow.
Make a separate cross link table that binds members to messages, so you can actually query by it. Then, you can add a field 'Responded' to that table to mark if a member responded.