0

I am trying to connection Django pagination with mysql Database but i am facing some problem

Problem

  1. it's work fine when data is less.
  2. but when data is in millions it take 2-3 minutes to load the data in html page, even i am fetching only 100 rows at a time.
  3. every time I change the page it take 2-3 minute to load the data in html page

view.py

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def displayLogs(request): from django.http import HttpResponseRedirect from django.shortcuts import renderimport mysql.connector

con = mysql.connector.connect (host="localhost", user = "root", passwd ="12345", db =  "database")

cur=con.cursor()




sql = "SELECT * FROM Table "
cur.execute(sql)
user_list=cur.fetchall()
searchlen=len(user_list)
paginator = Paginator(user_list, 100)
page = request.GET.get('page')
users = paginator.get_page(page)



return render(request,'displayLogs.html',{'users': users,'searchlen':searchlen})

template.html

<table cellpadding="0" cellspacing="0" border="0" class="table table table-striped table-hover table-bordered">
      <thead>
       <tr>
        <th ><center><b>ActivityDate</center></th>
        <th><center><b>Activity</center></th>
        <th><center><b>ActorUserPrinicipalName</center></th>
        <th><center><b>Target1Name</center></th>
       </tr>
      </thead>
      <tbody>
       <tr class="gradeX">
        {% for item in users %}
         <td>{{item.0}}</td>
         <td>{{item.1}}</td>
         <td>{{item.6}}</td>
         <td class="center">{{item.10}}</td>
        </tr>
        
       {% endfor %}
      </tbody>
      <tfoot>
      <tr>
       <th ><center><b>ActivityDate</center></th>
        <th><center><b>Activity</center></th>
        <th><center><b>ActorUserPrinicipalName</center></th>
        <th><center><b>Target1Name</center></th>
      </tr>
      </tfoot>
      

<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>
user10989738
  • 81
  • 1
  • 11
  • It seems that each time your fetching all rows from your table and then work with it. Is there any particular reason why don't you use Django ORM? – Sergey Pugach Mar 13 '19 at 13:09
  • Your query is fetching all the rows each time, which with millions of rows is going to take a long time. Is there a specific reason why you're using raw sql instead of Django's ORM? Django's ORM makes this easy to overcome. If you need to use raw sql you'll need to figure out how to programatically insert correct LIMIT and OFFSET statements into your SQL query – pistolpete Mar 13 '19 at 13:09
  • Don't know how to use ORM Can you help me with this? – user10989738 Mar 13 '19 at 13:27
  • How did you create tables? Do you have models? Read django docs here https://docs.djangoproject.com/en/2.1/intro/overview/ – Sergey Pugach Mar 13 '19 at 13:38

0 Answers0