title: Django slow load and PosgresSQL optimization
body: <p>I have build in app in Django and upload it to Heruko.
  I am using SQLPostgres of amazon.</p>
  <p>And I have installed the Newrelic plugin and Django Debug Tool.</p>
  <p>My problem is slow loading pages all over my app:
  I will show you part of my code of a very slow loading page.
  can you please point me on what I am doing wrong with my queries ?</p>
  <p><em>new relic:</em></p>
  <p><a href="https://i.stack.imgur.com/EuKKW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EuKKW.png" alt="enter image description here" /></a></p>
  <p><em>Django ToolBug:</em></p>
  <p><a href="https://i.stack.imgur.com/tTO2O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tTO2O.png" alt="enter image description here" /></a></p>
  <p><em>Views.py</em></p>
  <pre><code>@login_required(login_url='login')
  def students(request):
      form = StudentForm()
      form.fields['desc'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['firstname'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['lastname'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['sid'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['studentclass'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['email'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['phone'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['main'].widget.attrs['class'] = &quot;form-control&quot;
      form.fields['submain1'].widget.attrs['class'] = &quot;form-control&quot;
      students = Student.objects.all()
      if request.method == &quot;POST&quot;:
          form = StudentForm(request.POST)
          if form.is_valid():
              form.save()
              return redirect('students')
      return render(request, &quot;studentform/students.html&quot;, {&quot;form&quot;: form, &quot;students&quot;: students})
  </code></pre>
  <p><em>templete.html</em></p>
  <pre><code>{% extends 'studentform/controlpanel.html' %}
  {% load static %}
  {% block content %}
  &lt;div class=&quot;container&quot;&gt;
      &lt;h1&gt; תלמידים&lt;a style=&quot;float: left; padding-left: 20px&quot; href=&quot;{% url 'export_csv'%}&quot;&gt;&lt;img style=&quot;height: 35px;width: 35px&quot; src=&quot;https://d1yjjnpx0p53s8.cloudfront.net/styles/logo-thumbnail/s3/072015/excel_0_0.png?itok=tetyiA_8&quot;&gt;&lt;/a&gt;&lt;/h1&gt;
      &lt;div class=&quot;card shadow p-3 mb-5 bg-white rounded&quot;&gt;
          &lt;div class=&quot;card-body &quot;&gt;
      &lt;form id=&quot;students-form&quot; method=&quot;POST&quot;&gt;
          &lt;div class=&quot;row&quot;&gt;
              {% csrf_token %}
              {% for field in form %}
             {% if forloop.counter|divisibleby:&quot;3&quot; %}
              &lt;/div&gt;
          &lt;div style=&quot;padding-top: 10px&quot; class=&quot;row&quot;&gt;
  
              {% endif %}
              &lt;div class=&quot; col-md-4 col-sm-12 &quot;&gt;
                  &lt;label &gt;{{ field.label }}&lt;/label&gt;
                  {{ field }}
                  &lt;/div&gt;
  
              {% endfor %}
  
          &lt;/div&gt;
            &lt;div style=&quot;padding-top: 10px&quot; class=&quot;row justify-content-center text-center&quot;&gt;
              &lt;input type=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;צור תלמיד&quot; /&gt;
                  &lt;/div&gt;
      &lt;/form&gt;
  
  &lt;hr /&gt;
  
  &lt;div class=&quot;container&quot;&gt;
      &lt;table style=&quot;width:100%&quot;  class=&quot;display nowrap&quot; id=&quot;my_students&quot;&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;שם&lt;/th&gt;
                  &lt;th&gt;שם משפחה&lt;/th&gt;
                   &lt;th&gt;ת.ז&lt;/th&gt;
                  &lt;th&gt;איימיל&lt;/th&gt;
                  &lt;th&gt;טלפון&lt;/th&gt;
                  &lt;th&gt;כיתה&lt;/th&gt;
                  &lt;th&gt;מגמה ראשית&lt;/th&gt;
                  &lt;th&gt;חוגים&lt;/th&gt;
                  &lt;th&gt; עדכון&lt;/th&gt;
                  &lt;th&gt;מחיקה&lt;/th&gt;
  
  
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
          {% for i in students %}
          &lt;tr&gt;
              &lt;td&gt;{{i.firstname}}&lt;/td&gt;
              &lt;td&gt;{{i.lastname}}&lt;/td&gt;
              &lt;td&gt;{{i.sid}}&lt;/td&gt;
              &lt;td&gt;{{i.email}}&lt;/td&gt;
              &lt;td&gt;{{i.phone}}&lt;/td&gt;
              &lt;td&gt;{{i.studentclass.name}}&lt;/td&gt;
              &lt;td&gt;{{i.main.name}}&lt;/td&gt;
            &lt;td&gt;  {% for n in i.hog.all %}
                  {{n.name}}
                  {% endfor %}
  &lt;/td&gt;
              &lt;td&gt;&lt;a class=&quot;btn btn-sm btn-info&quot; href=&quot;{% url 'updatestudent' i.id %}&quot;&gt;עדכון&lt;/a&gt;&lt;/td&gt;
             &lt;!-- &lt;td&gt;&lt;a class=&quot;btn btn-sm btn-danger&quot; href=&quot;{% url 'deletestudent' i.id %}&quot;&gt;מחיקה&lt;/a&gt;&lt;/td&gt; --&gt;
              &lt;td&gt;&lt;button value=&quot;{{i.id}}&quot; class=&quot;btn btn-sm btn-danger deletestu&quot; href=&quot;#&quot;&gt;מחיקה&lt;/button&gt;&lt;/td&gt;
          &lt;/tr&gt;
          {% endfor %}
          &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
          &lt;/div&gt;
  &lt;/div&gt;
   &lt;/div&gt;
  {% endblock content %}
  </code></pre>
  <p>UPDATE:</p>
  <p>I use prefetch_releted and it seems to fix the 230 queries.</p>
  <p>but now i have :
  335.75 ms (15 queries including 8 similar and 8 duplicates )</p>
  <p>ms no change but the number of queries there is change.</p>
  <p><a href="https://i.stack.imgur.com/VyYCg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VyYCg.png" alt="enter image description here" /></a></p>