简体   繁体   English

mysql:设计实践

[英]mysql: design practices

I have tables setup as such: 我有这样的表设置:
A message is sent out to a group of users. 一条消息被发送给一组用户。

This message is put in the parent_message table 此消息放在parent_message表中
This table contains id | sender_id | date 该表包含id | sender_id | date id | sender_id | date

each message that is sent in that group is put in the child_message table 该组中发送的每个消息都放在child_message表中
this table contains id | parent_id | message | date_sent 该表包含id | parent_id | message | date_sent id | parent_id | message | date_sent

when a reply is received it is put into the reply_message table 收到答复后,将其放入reply_message表中
this table contains id | child_id | message | date_received 该表包含id | child_id | message | date_received id | child_id | message | date_received id | child_id | message | date_received . id | child_id | message | date_received

Now I have a few questions about this setup. 现在,我对此设置有一些疑问。


1) Every time the page is loaded I need to show how many child messages each parent message has. 1)每次加载页面时,我需要显示每个父消息有多少个子消息。
Would you add a column to the parent_message table called child_count or work it out in your query. 您是在parent_message表中添加一列称为child_count还是在查询中进行计算。
why, why not? 为什么,为什么不呢?

Example query 查询示例

 select *, count(select parent_id from child_message c where c.parent_id = p.parent_id ) child_count from parent_message; 

2) If the user chooses they can view all reply messages to a parent message. 2)如果用户选择,他们可以查看对父邮件的所有答复邮件。
would you add the parent_id to the reply reply_message table or work it out in your query? 您将parent_id添加到replyreply_message表中还是在查询中解决?
Why, why not? 为什么,为什么不呢?

Example query 查询示例

 select * from reply_message where child_id in(select id from child_message where parent_id = '66') 

You're probably better off working it out in both cases but I would rewrite the queries 您可能最好在两种情况下都解决它,但我会重写查询

SELECT 
   p.*
   count(child.*) childCount
FROM 
   parent_message p
   LEFT JOIN  child_message  c
   on c.parent_id = p.parent_id

and

SELECT DISTINCT
       rm.*
    FROM 
        reply_message rm
        INNER JOIN child_message  cm
        rm.child_id = cm.id 
   WHERE
       parent_id = '66'

I also would list the fields instead of doing the SELECT * 我也将列出字段而不是执行SELECT *

I'd say it very much depends on the amount of messages. 我会说这很大程度上取决于邮件的数量。 If you have like a million messages in the system, a join to child_message can become very expensive. 如果系统中有一百万条消息,则加入child_message可能会变得非常昂贵。 In that case adding a child_count to the parent table can be beneficial for your performance. 在这种情况下,将child_count添加到父表可能对您的性能有利。 Same goes for your second use case. 您的第二个用例也是如此。 Of course that is some de-normalization of your data, so if your system allows for reshaping topics and replies (like splitting a topic) you have to do additional bookkeeping in that case. 当然,这是数据的一些非规范化,因此,如果您的系统允许重塑主题和答复(例如拆分主题),则在这种情况下,您必须进行额外的记账。

Another approach would be creating index tables, which hold the information you need and update them offline in an asynchronous way, if you don't need the information to be 100% accurate all the time eg 另一种方法是创建索引表,如果您不需要信息始终保持100%的准确性,则该索引表将保存您需要的信息并以异步方式离线更新它们。

table message_counts (parent_id, child_count) 表message_counts(parent_id,child_count)

And then schedule updates on these when a new message is added to the system, eg by using a trigger. 然后,在将新消息添加到系统时(例如通过使用触发器)安排这些更新。

So bottom line, unless you encounter performance issues, keep your tables normalized, just like they are. 因此,最重要的是,除非遇到性能问题,否则请保持表正常化,就像它们一样。 When you expect millions of messages and replies, some de-normalization can help speed up things. 当您期望收到数以百万计的消息和答复时,某些反规范化可以帮助加快处理速度。 Index tables can help creating aggregated statistics offline, unless you need them to be accurate and up-to-date. 索引表可以帮助离线创建聚合统计信息,除非您需要它们是准确和最新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM