Reputation: 238
I'm running a website with Django and MySQL and I'm trying to optimize it. For that purpose I use the django debug toolbar and I pay a particular attention to the SQL part which is, I think, the reason why the load of my page is long.
When I look at the SQL part in django debug toolbar, it says that my queries take around 90ms in total. But if I put a timer around the rendering function in python, then it takes more than 3s (which is closer to what I actually see when reloading the page)...
How comes the query is that fast compared to the rendering?
EDIT: here is the code in the template simplified to its maximum:
<div id="content_annonces" class="ui-widget ui-corner-all">
<table>
{% if latest_annonces_list %}
here goes the content
{% else %}
nothing found
{% endif %}
</table>
</div>
as you can see, the only peace of code that is supposed to be costly is the SQL query requested when calling the object latest_annonces_list. But when I profile it with django debug toolbar it says this query has an elapsed of 90ms while the rendering takes around 3s ...
And here goes the content of latest_annonces_list:
result = Annonce.objects.select_related('vehicule', 'vehicule__marque')
.filter(vehicule__marque__in=self.marques).order_by('prix')[:10]
Models here:
class Marque(models.Model):
name = models.CharField(db_index=True, primary_key=True, max_length=100)
class Vehicule(models.Model):
modele = models.CharField(primary_key=True, db_index=True, max_length=100)
gen_modele = models.CharField(db_index=True, max_length=100)
marque = models.ForeignKey(Marque)
categories = models.ManyToManyField(Categorie)
click = models.PositiveIntegerField(default=0)
class Annonce(models.Model):
vehicule = models.ForeignKey(Vehicule, db_index=True)
porte = models.ForeignKey(Porte, db_index=True)
carburant = models.ForeignKey(Carburant, db_index=True)
bv = models.ForeignKey(Boite, db_index=True)
prix = models.DecimalField(db_index=True,max_digits=8, decimal_places=2)
km = models.PositiveIntegerField(db_index=True)
dpt = models.CharField(db_index=True, max_length=50)
annonceur = models.ForeignKey(Annonceur, db_index=True)
img = models.URLField()
url = models.URLField(max_length=2000)
finition = models.CharField(max_length=500)
cylindree = models.CharField(max_length=500)
moteur = models.CharField(max_length=500)
annee = models.DateTimeField(u'annee vehicule', db_index=True)
pub_date = models.DateTimeField(u'date publication')
last_touched = models.DateTimeField(u'derniere modification')
last_scan = models.DateTimeField(u'dernier scan')
type_ann = models.ForeignKey(Type_Annonce, db_index=True)
type_vendeur = models.ForeignKey(Vendeu
r, db_index=True)
Upvotes: 2
Views: 1578
Reputation: 583
90ms is still pretty much for a query executed against a local database, so I guess there are many joins.
General rule in such a situation is to reduce the ORM usage by:
values()
or values_list()
on your queryset, which will prevent Django ORM from building all the objects and executing relevant signalsProbably the quickest way to have a significant gain is either to cache the QuerySets using johnny-cache http://packages.python.org/johnny-cache/ or cache the output using {% cache ... %}
Upvotes: 1