From dc69f00094b47c52da3faf482e9b07c14c4b2807 Mon Sep 17 00:00:00 2001 From: Rauno Tuul Date: Mon, 13 Feb 2023 12:03:54 +0200 Subject: [PATCH] Fix dashboard MySQL performance with large history table and standard user privileges. --- powerdnsadmin/routes/dashboard.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/powerdnsadmin/routes/dashboard.py b/powerdnsadmin/routes/dashboard.py index 8cf1b12..b4952ff 100644 --- a/powerdnsadmin/routes/dashboard.py +++ b/powerdnsadmin/routes/dashboard.py @@ -166,6 +166,7 @@ def dashboard(): 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) \ .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ .outerjoin(Account, Domain.account_id == Account.id) \ @@ -175,9 +176,19 @@ def dashboard(): db.or_( DomainUser.user_id == current_user.id, AccountUser.user_id == current_user.id - )).all() - history_number = len(history) # history.count() - history = history[:4] + )) \ + .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() domain_count = db.session.query(Domain) \ .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ .outerjoin(Account, Domain.account_id == Account.id) \