简体   繁体   English

良好的数据库设计/规范化

[英]Good Database design/normalisation

I was assigned a task, with which I am not sure how to approach it: 我被分配了一个任务,我不确定该如何处理它:

I have to build a Messaging-System which supports multiple devices and it should be as efficient as possible. 我必须构建一个支持多个设备的消息系统,并且它应该尽可能高效。 Users can have up to 10 devices, which all need to receive a Message when the User receives it. 用户最多可以拥有10台设备,当用户收到消息时,所有这些设备都需要接收一条消息。

I have two Ideas: 我有两个想法:

Table Messages:
- ID (PK)
- SenderID
- ReceiverID
- Data

Table PendingTransmissions:
- MessageID (FK (PK of above table))
- DeviceID (FK)

The Problem here is that every time a message is sent, one entry per device is created and results in big overhead. 这里的问题是,每次发送消息时,每个设备都会创建一个条目,并且会导致大量开销。

Table Messages:
- ID (PK)
- SenderID
- ReceiverID
- Data
- ReceivedDevice1
- ReceivedDevice2
- ReceivedDevice3
- ReceivedDevice4
- ReceivedDevice5
- ReceivedDevice6
- ReceivedDevice7
- ReceivedDevice8
- ReceivedDevice9
- ReceivedDevice10

The problem here is obviously the redundance of Devices, but the overhead would be lower. 这里的问题显然是设备的冗余,但是开销会更低。

What is the better solution or is there something I have missed completely? 什么是更好的解决方案,或者我完全错过了什么?

Thanks in advance! 提前致谢!

The first approach is totally valid and it should be the way to go. 第一种方法是完全有效的,应该是可行的方法。 The maintainability of the second approach is a horror and on a later time when some code is added the code won't be very readable (i had to maintain multiple DBs already that where designed this way). 第二种方法的可维护性令人震惊,并且在以后添加一些代码时,该代码将不太可读(我必须维护以这种方式设计的多个DB)。

I would recommend against the 2nd approach for (at least) two reasons: 我建议使用第二种方法,至少有两个原因:

  • Today, one User can have 10 devices. 今天,一个用户可以拥有10台设备。 If that number changes to, say, 20 in future, that means changes in many layers in your application: database, application entity classes, DAO etc. 如果该数字将来更改为20,则意味着应用程序的许多层都发生了变化:数据库,应用程序实体类,DAO等。
  • In future, you might want to add functionality for groups. 将来,您可能想为组添加功能。 It'll be cumbersome to extend this design for that. 为此扩展这种设计将很麻烦。

In 1st approach, if you are concerned about your PendingTransmissions becoming too big in size, you can take care of it like this: 在第一种方法中,如果您担心PendingTransmissions大小过大,则可以像这样处理:

  • Add two columns: isDelivered and deliveredTimestamp . 添加两列: isDelivereddeliveredTimestamp You can then periodically archive all rows which are both delivered and older than, say, 1 month. 然后,您可以定期归档所有已交付且早于1个月的行。

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

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