has been a while i've been wondering how to optimize Django's queries
to the database, for example by setting it to use JOIN to retrieve
foreign keys instead of multiple selects.
for example if I have a blog object that has a category foreign key
and I write in a template
{{ blog.category.slug }}
I get another query to select the category.
Is there a way I can use a JOIN instead to avoid multiple queries?
Does this have sense for performance optimization?
Thanks
Federico
select_related() ?
http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4
~Rolando
how many things i've learnt today, to optimize the number and length
of query by using:
* select_related
* only
* extra
Do you think the performance gain is worth the work?
And I've a curiosity more to ask:
If I use the cache framework, once the results are cached the will the
database be hit again?
Best Regards
Federico
On Mar 31, 10:49 pm, Rolando Espinoza La Fuente <dark...@gmail.com>
wrote:
> On Wed, Mar 31, 2010 at 3:30 PM, Federico Capoano
>
Yes. Specially in loops where you have:
# in your view
posts = Post.objects.all()[:20]
# in your template
{% for post in posts %}
{{ post.title }}
{{ post.category.name }}
{{ post.owner.username }}
{% endfor %}
post.category and post.owner will hit the database each loop.
> And I've a curiosity more to ask:
> If I use the cache framework, once the results are cached the will the
> database be hit again?
No. But you need to take care of invalidation.
There are few apps that provide drop-out solution for caching at orm level:
* cache machine - http://jbalogh.me/2010/02/09/cache-machine/
* cachebot - http://blog.davidziegler.net/post/429237463/announcing-django-cachebot
* johnny cache - http://jmoiron.net/blog/is-johnny-cache-for-you/
Regards,
~Rolando
Thank you very much, I save these info on caching for future
reference.
On Apr 1, 12:49 am, Rolando Espinoza La Fuente <dark...@gmail.com>
wrote:
> On Wed, Mar 31, 2010 at 5:22 PM, Federico Capoano
>
http://superjared.com/projects/static-generator/
On Apr 1, 12:49 am, Rolando Espinoza La Fuente <dark...@gmail.com>
wrote:
> On Wed, Mar 31, 2010 at 5:22 PM, Federico Capoano
>
> <nemesis.des...@libero.it> wrote:
> > Thanks,
>
> > how many things i've learnt today, to optimize the number and length
> > of query by using:
>
> > * select_related
> > * only
> > * extra
>
> > Do you think the performance gain is worth the work?
>
> Yes. Specially in loops where you have:
>
> # in your view
> posts = Post.objects.all()[:20]
>
> # in your template
> {% for post in posts %}
> {{ post.title }}
> {{ post.category.name }}
> {{ post.owner.username }}
> {% endfor %}
>
> post.category and post.owner will hit the database each loop.
>
> > And I've a curiosity more to ask:
> > If I use the cache framework, once the results are cached the will the
> > database be hit again?
>
> No. But you need to take care of invalidation.
>
> There are few apps that provide drop-out solution for caching at orm level:
You also might be interested in "anotate" and "aggregate"
http://docs.djangoproject.com/en/dev/topics/db/aggregation/