Flask for loop very slow to load

Viewed 36

Im trying to figure out why one of my flask pages in my project is taking around 7 seconds to load.

Database is running on MySQL in Azure, if I run the query from VSCode interactive session against the same remote DB it takes 0.3s to come back.

When I test the app and launch the page, it takes around 7 seconds before the page is rendered.

def get_today():
    today = datetime.today().date()
    return today


@app.route('/leases/viewcurrent')
def viewCurrentLeases():
    today = get_today()
    current_leases = db.session.query(
        Leases_Model
    ).join(
        Property_Model
    ).join(
        Tenant_Model
    ).filter(
        Leases_Model.lease_enddate >= today
    ).all()
    
    return(render_template('viewLeases.html', current_leases = current_leases, Title = "Current Leases"))

The HTML page is like this

{% extends "base.html" %}


{% block content %}

<div class=".container">
    <div class="table-responsive">
    <table id="data" class="table table-hover">
        <thead>
            <tr>
                <th>Lease ID</th>
                <th>Property Address</th>
                <th>Current Tenant</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Actions</th>
            </tr>
        </thead>    
        <tbody>
            {% for lease in current_leases %}
            <tr>
                <td><a href="#">{{lease.lease_id}}</a></td>
                <td>{{lease.property.display_name}}</td>
                <td><a href="#">{{lease.tenant.display_name }}</a></td>
                <td>{{lease.lease_start_date}}</td>
                <td>{{lease.lease_enddate}}</td>
                <td><a href="{{url_for('rentView',lease_id=lease.lease_id)}}" class="button">Manage and View Rent</a></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    </div>
</div>
{% endblock %}

{% block scripts %}
  <script>
    $(document).ready(function () {
      console.log  
      $('#data').DataTable({
        columns: [
          null,
          {searchable: true},
          {searchable: true},
          null,
          null,
          null
        ],
      });
    });
  </script>
{% endblock %}

Any help is greatly appreciated as Im struggling to understand why its so slow. There are only 107 records in the query. I have tried with and without the jscript datable option but the same happens.

The query is with a join. I have other queries using different tables with. I joins and hundreds of records which appear very quickly.

I was previously using a sqllite file before moving to MySQL which is when the slowness started.

I’m not seeing anything in dev tools apart from the final page load of 7 seconds.

0 Answers
Related