Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango query fast while rendering slow
    primarykey
    data
    text
    <p>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.</p> <p>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)... </p> <p>How comes the query is that fast compared to the rendering?</p> <p>EDIT: here is the code in the template simplified to its maximum: </p> <pre><code>&lt;div id="content_annonces" class="ui-widget ui-corner-all"&gt; &lt;table&gt; {% if latest_annonces_list %} here goes the content {% else %} nothing found {% endif %} &lt;/table&gt; &lt;/div&gt; </code></pre> <p>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 ...</p> <p>And here goes the content of latest_annonces_list:</p> <pre><code>result = Annonce.objects.select_related('vehicule', 'vehicule__marque') .filter(vehicule__marque__in=self.marques).order_by('prix')[:10] </code></pre> <p>Models here:</p> <pre><code>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 </code></pre> <p>r, db_index=True)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload