TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Some quick Django optimisation lessons

96 pointsby pajjuabout 13 years ago

7 comments

lucian1900about 13 years ago
I've also run into O(n) and even O(n*m) # queries situations. They're entirely avoidable by simply sticking closer to the relational model than the object model. Sadly, this is almost always impossible with just Django's ORM. You very quickly end up writing unmanaged models and raw SQL.<p>SQLAlchemy is so much better when it comes to this, since things like joins are always explicit. However, it's rather hard to use it together with the Django ORM bits, especially the admin. One method I want to explore is replacing only the raw SQL bits with SQLAlchemy, and use its reflection to map to the Django ORM models.
评论 #3997627 未加载
评论 #3998485 未加载
评论 #3998578 未加载
yummyfajitasabout 13 years ago
Another thing you can do is build caching into managers.<p>Here is one such effort (shameless plug): <a href="https://github.com/stucchio/Guacamole" rel="nofollow">https://github.com/stucchio/Guacamole</a>
评论 #3996777 未加载
评论 #3996671 未加载
insinabout 13 years ago
Also worth looking at prefetch_related(), which landed in 1.4, which can be used to alleviate common O(n) situations:<p><a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.prefetch_related" rel="nofollow">https://docs.djangoproject.com/en/dev/ref/models/querysets/#...</a><p>I've also used this in the past to do something similar, grabbing the ids of all specified related fields (related to the same model type) and pulling them back in a single query, with the option to only grab certain columns as dicts instead of full model instances if you're only selecting additional data required for specific templates:<p><a href="http://djangosnippets.org/snippets/1117/" rel="nofollow">http://djangosnippets.org/snippets/1117/</a>
irfanabout 13 years ago
<i>"Django makes it extremely easy to generate database queries, which is both a good and bad thing"</i><p>I experienced this when a junior developer made queries in a loop inside template without realizing that it is actually going to be a lot of SQL queries in the end.
yaixabout 13 years ago
Related question: is there an easy build-in way in Django to write to stdout a count of DB queries made for a request?
评论 #3996635 未加载
评论 #3996617 未加载
评论 #3996666 未加载
gbogabout 13 years ago
Maybe it is just a matter of style but I really dislike when advices are repeated three times, I regard this as disrespectful of the reader.
idleloopsabout 13 years ago
That seems like an unholy amount of queries. Would be nice if there was an explanation as to the why behind the queries on those paths.