On 6/28/07, grassoalvaro <[EMAIL PROTECTED]> wrote: > > text: 1.149759 > name: 0.017819 > --------------------------------------------------------------------- > > Can someone explain me, why when i'm doing gettatr(test, "text") > (ManyToMany) i have so long delay? That is django (not database) > problem but i really don't know why. Anyone?
Your first line of code retrieves a TestField object instance. This populates the value of the 'name' attribute (because it is simple table data), so 10000 calls to getattr(test, 'name') is really just returning the same cached attribute value 10000 times. There is only ever 1 call on the database. However, each call to getattr(test, 'text') requires a call on the database, performing a join on the M2M related table. This takes some time (or at least, much more time than a simple attribute lookup), and there is 10000 calls on the database. The solution to optimizing this specific example is to do the getattr once, store the result, and use it multiple times. However, your example is a little artificial, so its difficult to tell the real problem that you are trying to optimize. As a side note, if your model used a ForeignKey, it would be possible to use select_related() to pre-cache related objects: http://www.djangoproject.com/documentation/db-api/#select-related However, this option isn't available to ManyToMany relations. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---