Merge pull request #2884 from ilyalabun/ilabun/optimize-certificates-sql

Optimize certificates sql
This commit is contained in:
Hossein Shafagh 2020-06-09 17:10:04 -07:00 committed by GitHub
commit 43bb48c286
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 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
@ -455,8 +456,8 @@ def render(args):
elif "cn" in terms: elif "cn" in terms:
query = query.filter( query = query.filter(
or_( or_(
Certificate.cn.ilike(term), func.lower(Certificate.cn).like(term.lower()),
Certificate.domains.any(Domain.name.ilike(term)), Certificate.id.in_(like_domain_query(term)),
) )
) )
elif "id" in terms: elif "id" in terms:
@ -464,9 +465,9 @@ def render(args):
elif "name" in terms: elif "name" in terms:
query = query.filter( query = query.filter(
or_( or_(
Certificate.name.ilike(term), func.lower(Certificate.name).like(term.lower()),
Certificate.domains.any(Domain.name.ilike(term)), Certificate.id.in_(like_domain_query(term)),
Certificate.cn.ilike(term), func.lower(Certificate.cn).like(term.lower()),
) )
) )
elif "fixedName" in terms: elif "fixedName" in terms:
@ -511,6 +512,14 @@ def render(args):
return result return result
def like_domain_query(term):
domain_query = database.session_query(Domain.id)
domain_query = domain_query.filter(func.lower(Domain.name).like(term.lower()))
assoc_query = database.session_query(certificate_associations.c.certificate_id)
assoc_query = assoc_query.filter(certificate_associations.c.domain_id.in_(domain_query))
return assoc_query
def query_name(certificate_name, args): def query_name(certificate_name, args):
""" """
Helper function that queries for a certificate by name Helper function that queries for a certificate by name

View File

@ -0,0 +1,50 @@
"""Add lowercase index for certificate name and cn and also for domain name
Revision ID: 8323a5ea723a
Revises: b33c838cb669
Create Date: 2020-01-10 10:51:44.776052
"""
# revision identifiers, used by Alembic.
revision = '8323a5ea723a'
down_revision = 'b33c838cb669'
from alembic import op
from sqlalchemy import text
import sqlalchemy as sa
def upgrade():
op.create_index(
"ix_certificates_cn_lower",
"certificates",
[text("lower(cn)")],
unique=False,
postgresql_ops={"lower(cn)": "gin_trgm_ops"},
postgresql_using="gin",
)
op.create_index(
"ix_certificates_name_lower",
"certificates",
[text("lower(name)")],
unique=False,
postgresql_ops={"lower(name)": "gin_trgm_ops"},
postgresql_using="gin",
)
op.create_index(
"ix_domains_name_lower",
"domains",
[text("lower(name)")],
unique=False,
postgresql_ops={"lower(name)": "gin_trgm_ops"},
postgresql_using="gin",
)
def downgrade():
op.drop_index("ix_certificates_cn_lower", table_name="certificates")
op.drop_index("ix_certificates_name_lower", table_name="certificates")
op.drop_index("ix_domains_name_lower", table_name="domains")

View File

@ -45,6 +45,6 @@ def upgrade():
def downgrade(): def downgrade():
op.drop_index("ix_domains_name", table_name="domains") op.drop_index("ix_domains_name_gin", table_name="domains")
op.drop_index("ix_certificates_name", table_name="certificates") op.drop_index("ix_certificates_name", table_name="certificates")
op.drop_index("ix_certificates_cn", table_name="certificates") op.drop_index("ix_certificates_cn", table_name="certificates")