Re: gettatr() access to ManyToMany huuuge delay?
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 1 calls to getattr(test, 'name') is really just returning the same cached attribute value 1 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 1 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 -~--~~~~--~~--~--~---
Re: gettatr() access to ManyToMany huuuge delay?
No, it doesn't depend on server. Like i said - this is django problem. On Jun 28, 1:37 am, l5x <[EMAIL PROTECTED]> wrote: > On Jun 27, 11:32 pm, grassoalvaro <[EMAIL PROTECTED]> wrote: > > > > > Here some code: > > - > > class TestField(models.Model): > > name = models.TextField() > > text = models.ManyToManyField(TextOption) > > class Meta: > > db_table = 'test' > > > test = TestField.objects.get() > > a = time.time() > > for i in range(0, 1): > > t = getattr(test, "text") > > print "text: %f"%(time.time()-a) > > a = time.time() > > for i in range(0, 1): > > t = getattr(test, "name") > > print "name: %f"%(time.time()-a) > > > 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? > > Doesn't it depend on server ? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: gettatr() access to ManyToMany huuuge delay?
On Jun 27, 11:32 pm, grassoalvaro <[EMAIL PROTECTED]> wrote: > Here some code: > - > class TestField(models.Model): > name = models.TextField() > text = models.ManyToManyField(TextOption) > class Meta: > db_table = 'test' > > test = TestField.objects.get() > a = time.time() > for i in range(0, 1): > t = getattr(test, "text") > print "text: %f"%(time.time()-a) > a = time.time() > for i in range(0, 1): > t = getattr(test, "name") > print "name: %f"%(time.time()-a) > > 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? Doesn't it depend on server ? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---