Merge pull request #2062 from castrapel/optimize_filter_queries

Optimize certificate filtering by name
This commit is contained in:
Curtis 2018-11-08 08:40:01 -08:00 committed by GitHub
commit de1c2fc500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -20,6 +20,7 @@ from lemur.common.utils import generate_private_key, truthiness
from lemur.destinations.models import Destination from lemur.destinations.models import Destination
from lemur.domains.models import Domain from lemur.domains.models import Domain
from lemur.extensions import metrics, sentry, signals from lemur.extensions import metrics, sentry, signals
from lemur.models import certificate_associations
from lemur.notifications.models import Notification from lemur.notifications.models import Notification
from lemur.pending_certificates.models import PendingCertificate from lemur.pending_certificates.models import PendingCertificate
from lemur.plugins.base import plugins from lemur.plugins.base import plugins
@ -332,13 +333,13 @@ def render(args):
elif 'id' in terms: elif 'id' in terms:
query = query.filter(Certificate.id == cast(terms[1], Integer)) query = query.filter(Certificate.id == cast(terms[1], Integer))
elif 'name' in terms: elif 'name' in terms:
query = query.filter( query = query.join(certificate_associations).join(Domain).filter(
or_( or_(
Certificate.name.ilike(term), Certificate.name.ilike(term),
Certificate.domains.any(Domain.name.ilike(term)), Domain.name.ilike(term),
Certificate.cn.ilike(term), Certificate.cn.ilike(term),
) )
) ).group_by(Certificate.id)
else: else:
query = database.filter(query, Certificate, terms) query = database.filter(query, Certificate, terms)

View File

@ -11,11 +11,11 @@
""" """
from inflection import underscore from inflection import underscore
from sqlalchemy import exc, func from sqlalchemy import exc, func
from sqlalchemy.sql import and_, or_
from sqlalchemy.orm import make_transient from sqlalchemy.orm import make_transient
from sqlalchemy.sql import and_, or_
from lemur.extensions import db
from lemur.exceptions import AttrNotFound, DuplicateError from lemur.exceptions import AttrNotFound, DuplicateError
from lemur.extensions import db
def filter_none(kwargs): def filter_none(kwargs):
@ -273,7 +273,7 @@ def get_count(q):
:param q: :param q:
:return: :return:
""" """
count_q = q.statement.with_only_columns([func.count()]).order_by(None) count_q = q.statement.with_only_columns([func.count()]).group_by(None).order_by(None)
count = q.session.execute(count_q).scalar() count = q.session.execute(count_q).scalar()
return count return count