Reputation: 4651
I have a model "Messages" which I use to store messages throughout the site. These are messages in discussions, private messages and probably chat. They are all stored in one table. I wonder if it will be faster if I spread messages among several models and tables. One for chat, one for discussions and so on.
So should I keep all messages in one table/model or create several identical models/tables?
Upvotes: 1
Views: 1045
Reputation: 50602
As long as you have an index on your type
column and filter on that, it will be about the same speed. When your table gets really big, just shard on the type
column and it will be the same performance as doing multiple tables but your app will just see one big table.
Upvotes: 1
Reputation: 24933
One "Table" will be better for search purposes (you can "search" on all of the messages at once.
However, multiple tables may benefit from speed.
Why not use abstracted classes?
class MessageBase(models.Model):
subject = models.CharField(max_length=255)
test = models.TextField()
class ChatMessage(MessageBase):
pass
This will create 2 tables, with the table for ChatMessage just referring directly to the table for MessageBase. This will give you the best of both worlds. "Search" using MessageBase to get messages for anything, but save, and refer to, all other messages using it's specific model class.
(please note, the python here might be slightly wrong, as it hasn't been tested, but I'm sure you get the idea!)
Upvotes: 1