List Info

Thread: optimizing queries




optimizing queries
user name
2007-01-29 04:11:56
Hi,
sometimes I find myself in a need of raw sql, for example
when joining 
tables that don't have explicit related fields.
In those cases "extra" method comes in really
handy. For example I 
have following models.

class TagName(models.Model):
   name = model.CharField()

class Tag(models.Model):
   content_type = model.ForeignKey(ContentType)
   object_id  = model.IntegerField()
   tags = model.ManyToManyField(TagName)

class WhateverObject(models.Model)
   ...
   owner_id = model.ForeignKey(User)
   ...

And I want to get all "TagName"s of objects in a
model owned by 
particular user. Getting only names is easily done extending
'tables' 
and 'where' part of the "extra" method, but when
it comes to counting 
the number of objects tagged by one tag, I miss the
"group by" part of 
the select statement.

One of the solutions would be to return all TagNames and
then count 
them within the python, and the other would be to fall
completely to 
sql in Manager of "TagName". Somehow it seems that
using only SQL 
would be quicker, but at the same time less readable and not
database 
independent.

What do django-developers usually prefer?

Mladen


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: optimizing queries
user name
2007-01-29 05:34:57
I think using SQL could be quite database independant though
less 
readable. Anyway, executing a count query for each
"TagName" is not 
acceptable. So if there's no "django way" to do
that, I would 
recommand SQL


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )