Django缓慢加载和PosgresSQL优化

问题描述

我已经在Django中构建了应用程序,并将其上传到Heruko。 我正在使用亚马逊的sqlPostgres。

我已经安装了Newrelic插件和Django Debug Tool。

我的问题是整个应用程序的页面加载速度缓慢: 我将向您展示加载速度非常慢的页面的部分代码。 您能指出我在查询中做错什么吗?

新遗物:

enter image description here

Django ToolBug:

enter image description here

Views.py

@login_required(login_url='login')
def students(request):
    form = StudentForm()
    form.fields['desc'].widget.attrs['class'] = "form-control"
    form.fields['firstname'].widget.attrs['class'] = "form-control"
    form.fields['lastname'].widget.attrs['class'] = "form-control"
    form.fields['sid'].widget.attrs['class'] = "form-control"
    form.fields['studentclass'].widget.attrs['class'] = "form-control"
    form.fields['email'].widget.attrs['class'] = "form-control"
    form.fields['phone'].widget.attrs['class'] = "form-control"
    form.fields['main'].widget.attrs['class'] = "form-control"
    form.fields['submain1'].widget.attrs['class'] = "form-control"
    students = Student.objects.all()
    if request.method == "POST":
        form = StudentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('students')
    return render(request,"studentform/students.html",{"form": form,"students": students})

templete.html

{% extends 'studentform/controlpanel.html' %}
{% load static %}
{% block content %}
<div class="container">
    <h1> תלמידים<a style="float: left; padding-left: 20px" href="{% url 'export_csv'%}"><img style="height: 35px;width: 35px" src="https://d1yjjnpx0p53s8.cloudfront.net/styles/logo-thumbnail/s3/072015/excel_0_0.png?itok=tetyiA_8"></a></h1>
    <div class="card shadow p-3 mb-5 bg-white rounded">
        <div class="card-body ">
    <form id="students-form" method="POST">
        <div class="row">
            {% csrf_token %}
            {% for field in form %}
           {% if forloop.counter|divisibleby:"3" %}
            </div>
        <div style="padding-top: 10px" class="row">

            {% endif %}
            <div class=" col-md-4 col-sm-12 ">
                <label >{{ field.label }}</label>
                {{ field }}
                </div>

            {% endfor %}

        </div>
          <div style="padding-top: 10px" class="row justify-content-center text-center">
            <input type="submit" class="btn btn-primary" value="צור תלמיד" />
                </div>
    </form>

<hr />

<div class="container">
    <table style="width:100%"  class="display Nowrap" id="my_students">
        <thead>
            <tr>
                <th>שם</th>
                <th>שם משפחה</th>
                 <th>ת.ז</th>
                <th>איימיל</th>
                <th>טלפון</th>
                <th>כיתה</th>
                <th>מגמה ראשית</th>
                <th>חוגים</th>
                <th> עדכון</th>
                <th>מחיקה</th>


            </tr>
        </thead>
        <tbody>
        {% for i in students %}
        <tr>
            <td>{{i.firstname}}</td>
            <td>{{i.lastname}}</td>
            <td>{{i.sid}}</td>
            <td>{{i.email}}</td>
            <td>{{i.phone}}</td>
            <td>{{i.studentclass.name}}</td>
            <td>{{i.main.name}}</td>
          <td>  {% for n in i.hog.all %}
                {{n.name}}
                {% endfor %}
</td>
            <td><a class="btn btn-sm btn-info" href="{% url 'updatestudent' i.id %}">עדכון</a></td>
           <!-- <td><a class="btn btn-sm btn-danger" href="{% url 'deletestudent' i.id %}">מחיקה</a></td> -->
            <td><button value="{{i.id}}" class="btn btn-sm btn-danger deletestu" href="#">מחיקה</button></td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
        </div>
</div>
 </div>
{% endblock content %}

更新:

我使用prefetch_releted,看来可以解决230个查询

但是现在我有: 335.75毫秒(15个查询包括8个相似和8个重复)

ms没有变化,但是查询数量有所变化。

enter image description here

解决方法

问题取决于您的数据库模型。 我猜,specialitysubdomain被设置为foreign keys模型的student。 在那种情况下,如果需要加载student模型,则重复调用与外键相关的模型会加重负载。 select_related()prefetch_related()选项可减少重复加载,因为它会立即加载与模型关联的外键。