messages table
The messages table in the Social Media Database Schema schema has 8 columns, keyed by id, with 3 foreign keys to conversations, users and messages. 2 columns in message_attachments and message_reads reference it.
The composite index on conversation and time is the single most important index in a chat system.
Columns (8)
| Column | Type | Constraints | Description |
|---|---|---|---|
| id | uuid | primary key | |
| conversation_id | uuid | not null, references conversations.id | |
| sender_id | uuid | not null, references users.id | |
| reply_to_id | uuid | references messages.id | |
| body | varchar(4000) | ||
| created_at | timestamptz | not null | |
| edited_at | timestamptz | ||
| deleted_at | timestamptz |
Indexes (1)
(conversation_id, created_at)
Relationships
messages.conversation_idreferencesconversations.idmessages.sender_idreferencesusers.idmessages.reply_to_idreferencesmessages.idmessage_attachments.message_idreferencesmessagesmessage_reads.last_read_message_idreferencesmessages