Subject | Email system - read/unread messages database design ... |
---|---|
Author | westsorkin |
Post date | 2007-01-04T21:53:18Z |
I am stuck with the database design of a kind of email messaging
system. More precisely with the design of the read/unread messages
feature. Note the system special feature: one message can have (and
will have) multiple recipients. Thus I decided to put all messages in
their own table, and organize them with the UserMessages table.
Here is the current simplified design:
User(ID, Name, etc),
Messages(ID, TimeStamp, Subject, Body)
UserMessages(ID, MessageID, UserID, IsRead)
I will have the following simplfied query:
select
Message.id, Message.Subject
from
Messages
inner join UserMessages on (UserMessages.MessageID = Messages.ID)
where
(:show_only_unread = 'YES' and UserMessages.IsRead = 'NO') or
(:show_only_unread = 'NO')
Questions:
1. Is that a good design? My other idea was instead having
UserMessages(ID, MessageID, UserID, IsRead), to have only
UserMessages(ID, MessageID, UserID), and then to have two tables:
UserMessagesRead(ID, UserMessagesID)
UserMessagesUnRead(ID, UserMessagesID)
This way I will have faster query for ShowUnread messages, faster
query for ShowAll messages ... I think...
But every time when I have to mark a message as read, I will have to:
1) insert it into readed messages table. And 2) Delete it from
UnreadMessage table. Or vise versa when markng message as not read.
2. What indexes I have to define for that kind of design? I will have
to sort messages by the TimeStamp filed too both descending and ascending.
Any comments greatly appreciated!
system. More precisely with the design of the read/unread messages
feature. Note the system special feature: one message can have (and
will have) multiple recipients. Thus I decided to put all messages in
their own table, and organize them with the UserMessages table.
Here is the current simplified design:
User(ID, Name, etc),
Messages(ID, TimeStamp, Subject, Body)
UserMessages(ID, MessageID, UserID, IsRead)
I will have the following simplfied query:
select
Message.id, Message.Subject
from
Messages
inner join UserMessages on (UserMessages.MessageID = Messages.ID)
where
(:show_only_unread = 'YES' and UserMessages.IsRead = 'NO') or
(:show_only_unread = 'NO')
Questions:
1. Is that a good design? My other idea was instead having
UserMessages(ID, MessageID, UserID, IsRead), to have only
UserMessages(ID, MessageID, UserID), and then to have two tables:
UserMessagesRead(ID, UserMessagesID)
UserMessagesUnRead(ID, UserMessagesID)
This way I will have faster query for ShowUnread messages, faster
query for ShowAll messages ... I think...
But every time when I have to mark a message as read, I will have to:
1) insert it into readed messages table. And 2) Delete it from
UnreadMessage table. Or vise versa when markng message as not read.
2. What indexes I have to define for that kind of design? I will have
to sort messages by the TimeStamp filed too both descending and ascending.
Any comments greatly appreciated!