On Thu, Sep 4, 2014 at 12:22 AM, msoulier <
msou...@digitaltorque.ca> wrote:
> Hi,
>
> I am looking at Django's performance with respect to modifying large numbers
> of objects, each in a unique way that cannot be batched. If I make a simple
> change to one of my Django models and save(), and then do the same thing in
> sqlalchemy, I notice a performance difference of about 48 times as far as
> the rate that the objects are processed to my postgresql db.
>
> The code is a simple property update and save, in a loop, trying to process
> as many objects as possible.
Is the update invariant? By using the ORM like this:
for obj in MyObject.objects.all():
obj.foo = 'hello'
obj.save()
then you have to pull all the data for each object out of the
database, convert the raw DB column to it's python type, assign it to
a model instance, convert each python field back to its raw DB value
and save it in the database. That is N+1 queries, and many
conversions.
If the update is invariant, you can apply it without any of the
overhead, only 1 query and one conversion:
MyObject.objects.all().update(foo='hello')
If the update doesn't depend on the other fields in the model, this
avoids some of the overhead, still N+1 queries but virtually no
conversion overhead:
for pk in MyObject.objects.all().values_list('pk', flat=True):
MyObject.objects.filter(pk=pk).update(foo=make_new_foo())
>
> Is the Django ORM known to be slower in this regard, or is it likely
> something that I'm doing?
Are both Django and the sqlalchemy doing the same sort of update?
Cheers
Tom