From ec687b13a5831dbfea72b4adc77039eb9f792c6c Mon Sep 17 00:00:00 2001 From: Rauno Tuul Date: Wed, 1 Mar 2023 11:42:21 +0200 Subject: [PATCH] Dashboard history query performance related change to nested subquery (allowed domains for user). --- powerdnsadmin/routes/dashboard.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/powerdnsadmin/routes/dashboard.py b/powerdnsadmin/routes/dashboard.py index b4952ff..d75ce50 100644 --- a/powerdnsadmin/routes/dashboard.py +++ b/powerdnsadmin/routes/dashboard.py @@ -165,30 +165,24 @@ def dashboard(): history_number = History.query.count() history = History.query.order_by(History.created_on.desc()).limit(4).all() elif Setting().get('allow_user_view_history'): - history = db.session.query(History) \ - .with_hint(History, "FORCE INDEX (ix_history_created_on)", 'mysql') \ - .join(Domain, History.domain_id == Domain.id) \ + allowed_domain_id_subquery = db.session.query(Domain.id) \ .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ .outerjoin(Account, Domain.account_id == Account.id) \ .outerjoin(AccountUser, Account.id == AccountUser.account_id) \ - .order_by(History.created_on.desc()) \ - .filter( - db.or_( + .filter(db.or_( DomainUser.user_id == current_user.id, AccountUser.user_id == current_user.id )) \ + .subquery() + history = db.session.query(History) \ + .with_hint(History, "FORCE INDEX (ix_history_created_on)", 'mysql') \ + .order_by(History.created_on.desc()) \ + .filter(History.domain_id.in_(allowed_domain_id_subquery)) \ .limit(4) \ .all() history_number = db.session.query(History) \ - .join(Domain, History.domain_id == Domain.id) \ - .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ - .outerjoin(Account, Domain.account_id == Account.id) \ - .outerjoin(AccountUser, Account.id == AccountUser.account_id) \ - .filter( - db.or_( - DomainUser.user_id == current_user.id, - AccountUser.user_id == current_user.id - )).count() + .filter(History.domain_id.in_(allowed_domain_id_subquery)) \ + .count() domain_count = db.session.query(Domain) \ .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ .outerjoin(Account, Domain.account_id == Account.id) \