From 08a2a2b0e566f4acadedd634c7905dea14393da2 Mon Sep 17 00:00:00 2001 From: Curtis Castrapel Date: Wed, 7 Nov 2018 15:34:25 -0800 Subject: [PATCH 1/2] Optimize certificate filtering by name --- lemur/certificates/service.py | 5 +++-- lemur/database.py | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lemur/certificates/service.py b/lemur/certificates/service.py index 8fc031c4..6deeecfa 100644 --- a/lemur/certificates/service.py +++ b/lemur/certificates/service.py @@ -20,6 +20,7 @@ from lemur.common.utils import generate_private_key, truthiness from lemur.destinations.models import Destination from lemur.domains.models import Domain from lemur.extensions import metrics, sentry, signals +from lemur.models import certificate_associations from lemur.notifications.models import Notification from lemur.pending_certificates.models import PendingCertificate from lemur.plugins.base import plugins @@ -332,13 +333,13 @@ def render(args): elif 'id' in terms: query = query.filter(Certificate.id == cast(terms[1], Integer)) elif 'name' in terms: - query = query.filter( + query = query.join(certificate_associations).join(Domain).filter( or_( Certificate.name.ilike(term), Certificate.domains.any(Domain.name.ilike(term)), Certificate.cn.ilike(term), ) - ) + ).group_by(Certificate.id) else: query = database.filter(query, Certificate, terms) diff --git a/lemur/database.py b/lemur/database.py index ad3899aa..5cfe340a 100644 --- a/lemur/database.py +++ b/lemur/database.py @@ -11,11 +11,11 @@ """ from inflection import underscore from sqlalchemy import exc, func -from sqlalchemy.sql import and_, or_ 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.extensions import db def filter_none(kwargs): @@ -273,7 +273,7 @@ def get_count(q): :param q: :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() return count From 1643650685a337d852e3895ac47b66f214748499 Mon Sep 17 00:00:00 2001 From: Curtis Castrapel Date: Wed, 7 Nov 2018 16:02:04 -0800 Subject: [PATCH 2/2] Changing essential part of query --- lemur/certificates/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lemur/certificates/service.py b/lemur/certificates/service.py index 6deeecfa..6f2e4d9d 100644 --- a/lemur/certificates/service.py +++ b/lemur/certificates/service.py @@ -336,7 +336,7 @@ def render(args): query = query.join(certificate_associations).join(Domain).filter( or_( Certificate.name.ilike(term), - Certificate.domains.any(Domain.name.ilike(term)), + Domain.name.ilike(term), Certificate.cn.ilike(term), ) ).group_by(Certificate.id)