mysql:设计实践
我有这样的表格设置:
一条消息被发送给一组用户。
此消息放入 parent_message
表
该表包含 id |发件人 ID | date
该组中发送的每条消息都会放入 child_message
表中
该表包含 id |父 ID |留言 | date_sent
收到回复后,会将其放入 reply_message
表
该表包含 id |儿童 ID |留言 |接收日期
。
现在我对这个设置有几个问题。
1) Every time the page is loaded I need to show how many child messages each parent message has.
Would you add a column to the parent_message table called child_count or work it out in your query.
why, why not?
示例查询
select *,
count(select parent_id from child_message c where c.parent_id = p.parent_id ) child_count
from parent_message;
2) 如果用户选择,他们可以查看父消息的所有回复消息。
您会将parent_id 添加到回复reply_message 表中还是在查询中计算出来?
为什么,为什么不呢?
示例查询
select * from reply_message
where child_id in(select id from child_message where parent_id = '66')
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
This table contains id | sender_id | date
each message that is sent in that group is put in the child_message
table
this table contains id | parent_id | message | date_sent
when a reply is received it is put into the reply_message
table
this table contains 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.
Would you add a column to the parent_message table called child_count or work it out in your query.
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.
would you add the parent_id to the reply reply_message table or work it out in your query?
Why, why not?
Example query
select * from reply_message
where child_id in(select id from child_message where parent_id = '66')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说这很大程度上取决于消息的数量。如果系统中有大约一百万条消息,则连接到 child_message 可能会变得非常昂贵。在这种情况下,向父表添加 child_count 可能有利于您的性能。您的第二个用例也是如此。当然,这是对数据的某种非规范化,因此如果您的系统允许重塑主题和回复(例如拆分主题),那么在这种情况下您必须进行额外的簿记。
另一种方法是创建索引表,它保存您需要的信息并以异步方式离线更新它们,如果您不需要信息始终 100% 准确,例如
表 message_counts (parent_id, child_count)
然后安排当新消息添加到系统时,例如通过使用触发器,对这些进行更新。
因此,底线是,除非遇到性能问题,否则请保持表正常化,就像它们本来的样子一样。当您预计有数百万条消息和回复时,一些非规范化可以帮助加快速度。索引表可以帮助离线创建聚合统计信息,除非您需要它们准确且最新。
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. In that case adding a child_count to the parent table can be beneficial for your performance. 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 e.g.
table message_counts (parent_id, child_count)
And then schedule updates on these when a new message is added to the system, e.g. 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.
在这两种情况下,您可能最好都解决它,但我会重写查询
,并且
还会列出字段,而不是执行
SELECT *
You're probably better off working it out in both cases but I would rewrite the queries
and
I also would list the fields instead of doing the
SELECT *