mercredi 25 février 2015

Django queryset update performance and optimization

I've create a bulk delete function that deletes all enabled items. I've tried deleting 5000 records with the following statement



Item.objects.filter(owner=request.user.profile, enabled=True, is_active=True).update(is_active=False)


But it is painfully slow and I'm afraid that this is causing my server to run out of memory.


I've previously had the following and it was still quite slow.



items = Item.objects.filter(owner=request.user.profile, enabled=True, is_active=True)
for item in items:
item.is_active = False
item.save()


The database being used is SQLite and I am using Django 1.7.


I wish to optimize this operation as much as possible. Any pointers or good query optimization docs would be appreciated.


Aucun commentaire:

Enregistrer un commentaire