Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way to copy data from one table to another in Django?

Tags:

sql

django

I have two models -

ChatCurrent - (which stores the messages for the current active chats)
ChatArchive - (which archives the messages for the chats that have ended)

The reason I'm doing this is so that the ChatCurrent table always has minimum number of entries, making querying the table fast (I don't know if this works, please let me know if I've got this wrong)

So I basically want to copy (cut) data from the ChatCurrent to the ChatArchive model. What would be the fastest way to do this. From what I've read online, it seems that I might have to execute a raw SQL query, if you would be kind enough to even state the Query I'll be grateful.

Additional details - Both the models have the same schema.

like image 484
Sussagittikasusa Avatar asked Oct 15 '25 00:10

Sussagittikasusa


1 Answers

My opinion is that today they are not reason to denormalize database in this way to improve performance. Indexes or partitioning + indexes should be enought.

Also, in case that, for semantic reasons, you prefer have two tables (models) like: Chat and ChatHistory (or ChatCurrent and ChatActive) as you say and manage it with django, I thing that the right way to keep consistence is to create ToArchive() method in ChatCurrent. This method will move chat entries to historical chat model. You can perform this operation in background mode, then you can thread the swap in a celery process, in this way online users avoid wait for request. Into celery process the fastest method to copy data is a raw sql. Remember that you can encapsulate sql into a stored procedure.

Edited to include reply to your comment

You can perform ChatCurrent.ToArchive() in ChatCurrent.save() method:

class ChatCurrent(model.Model):
    closed=models.BooleanField()

    def save(self, *args, **kwargs):
        super(Model, self).save(*args, **kwargs)
        if self.closed:
            self.ToArchive()

    def ToArchive(self):
        from django.db import connection, transaction
        cursor = connection.cursor()            
        cursor.execute("insert into blah blah")
        transaction.commit_unless_managed()
        #self.delete()  #if needed (perhaps deleted on raw sql)
like image 97
dani herrera Avatar answered Oct 17 '25 14:10

dani herrera