当数据集太大时,Django分页工作非常缓慢

时间:2019-03-15 08:54:19

标签: python django django-models django-templates django-pagination

当数据集非常大(> 100k)时,Django分页非常慢 我的数据集是80列,超过10万行。 当我加载页面或更改页面时,它需要14-15秒 有什么我可以做的来加快它的速度
我该怎么做才能使其快速。请为此提供帮助

  

model.py

class Finaltest3(models.Model):
a = models.DateTimeField(db_column='Dat',primary_key=True, blank=True, null=False)  # Field name made lowercase.
b = models.TextField(db_column='C', blank=True, null=True)  # Field name made lowercase.
c = models.TextField(db_column='S', blank=True, null=True)  # Field name made lowercase.
d = models.TextField(db_column='Ca', blank=True, null=True)  # Field name made lowercase.
e = models.TextField(db_column='Ac', blank=True, null=True)  # Field name made lowercase.
f = models.TextField(db_column='Re', blank=True, null=True)  # Field name made lowercase.
 .
 .
 .
 .
 .
fd = models.TextField(db_column='Add', blank=True, null=True)  # Field name made lowercase.
fd= models.TextField(db_column='Addit', blank=True, null=True)  # Field name made lowercase.
fd = models.TextField(db_column='Additional', blank=True, null=True)  # Field name made lowercase.

class Meta:
    managed = False
    db_table = 'finaltest3'
  

view.py

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import datetime
from datetime import date
from datetime import timedelta
from django.http import HttpResponseRedirect
from django.shortcuts import render
def displayLogs(request):
  today = date.today() 
  lastweek=today - timedelta(days=7)
  print('lastweek',lastweek)    
  user_list = Finaltest3.objects.filter(dat__gt=lastweek)
  paginator = Paginator(user_list, 10)
  page = request.GET.get('page')
  users = paginator.get_page(page)
return render(request,'displayLogs.html',{'users':users})
  

template.html

<div class="pagination" style="border-style:inset;border-width:5px;padding-bottom: 5px">
{% if users.has_previous %}

<a class="pagination-action" href="?page=1"> <i class="fas fa-angle-double-left"></i></a>

<a class="pagination-action" href="?page={{ users.previous_page_number}}"> <i class="fas fa-angle-left"></i></a>

{% endif %}

{% for num in users.paginator.page_range %}
    {% if users.number == num %}

        <span class="pagination-number pagination-current"><strong>{{ num }}</strong></span>

    {% elif num > users.number|add:'-3' and num < users.number|add:'3' %}

    <a class="pagination-number" href="?page={{ num }}">{{num}}</a>

    {% endif %}

{% endfor %}

{% if users.has_next %}

<a class="pagination-action" href="?page={{ users.next_page_number }}"><i class="fas fa-angle-right"></i></a>
<a class="pagination-action" href="?page={{ users.paginator.num_pages }}"> <i class="fas fa-angle-double-right"> </i></a>

{% endif %}

</div>
<tbody>
                        <tr class="gradeX">
                            {% for item in users %}
                                <td>{{item.dat}}</td>
                                <td>{{item.act}}</td>
                                <td>{{item.actor}}</td>
                                <td class="center">{{item.target}}</td>
                            </tr>

                        {% endfor %}
</tbody>

1 个答案:

答案 0 :(得分:1)

我认为速度缓慢是由于以下原因:

  • 您要过滤的字段上可能没有数据库索引-您正在使用的数据库系统必须整理出所有数据以进行lastweek字段过滤
  • 请记住,您可以打印Django正在生成的查询:print(Finaltest3.objects.filter(dat__gt=lastweek).query)
  • 您要获取数据的所有列,也许可以将字段限制为所需字段-请查看only and defer