06/08 Optimize queries with indexes
This commit is contained in:
@@ -24,6 +24,7 @@ from app.models.notification import (
|
||||
)
|
||||
from app.utils.audit import log_action, ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE, ACTION_EXPORT
|
||||
from app.utils.scope import get_customer_scope, get_inspector_scope
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
bp = Blueprint('inspections', __name__, url_prefix='/inspections')
|
||||
|
||||
@@ -198,7 +199,12 @@ def _validate_required(form_fields, responses):
|
||||
@login_required
|
||||
def index():
|
||||
page = request.args.get('page', 1, type=int)
|
||||
q = Inspection.query.order_by(Inspection.inspection_date.desc())
|
||||
q = Inspection.query.options(
|
||||
joinedload(Inspection.facility),
|
||||
joinedload(Inspection.template),
|
||||
joinedload(Inspection.inspector),
|
||||
joinedload(Inspection.area),
|
||||
).order_by(Inspection.inspection_date.desc())
|
||||
|
||||
if current_user.role == 'inspector':
|
||||
fids = get_inspector_scope(current_user)
|
||||
|
||||
@@ -19,6 +19,7 @@ from app.utils.notifications import notify, notify_customers_for_facility, notif
|
||||
from app.utils.audit import log_action, ACTION_CREATE, ACTION_UPDATE, ACTION_DELETE
|
||||
from app.utils.scope import get_customer_scope, get_inspector_scope
|
||||
from app.utils.sla import sla_status
|
||||
from sqlalchemy.orm import joinedload, contains_eager
|
||||
|
||||
bp = Blueprint('issues', __name__, url_prefix='/issues')
|
||||
|
||||
@@ -76,6 +77,11 @@ def index():
|
||||
q = (
|
||||
Issue.query
|
||||
.outerjoin(Area, Issue.area_id == Area.id)
|
||||
.options(
|
||||
contains_eager(Issue.area),
|
||||
joinedload(Issue.facility),
|
||||
joinedload(Issue.assigned_user),
|
||||
)
|
||||
.order_by(Issue.reported_at.desc())
|
||||
)
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ from flask import (Blueprint, render_template, request,
|
||||
Response, stream_with_context, abort)
|
||||
from flask_login import login_required, current_user
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import joinedload
|
||||
from app import db
|
||||
from app.models.inspection import Inspection, InspectionTemplate
|
||||
from app.models.facility import Facility, Area
|
||||
@@ -677,6 +678,11 @@ def inspector_performance():
|
||||
|
||||
recent_inspections = (
|
||||
Inspection.query
|
||||
.options(
|
||||
joinedload(Inspection.facility),
|
||||
joinedload(Inspection.area),
|
||||
joinedload(Inspection.template),
|
||||
)
|
||||
.filter(
|
||||
Inspection.inspector_id == selected_id,
|
||||
Inspection.inspection_date >= start,
|
||||
|
||||
Reference in New Issue
Block a user