views:

96

answers:

2

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 1 table. I wonder if it will be faster if i spread messages among several models and tables. 1 for chat, one for discussions and so on.

So should i keep all messages in 1 table/model or create several identical models/tables?

A: 

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!)

Mez
+1  A: 

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.

Paul Tarjan