Merge pull request #1379 from raunz/dashboard_improvements

Fix dashboard MySQL performance with large history for standard users
This commit is contained in:
Matt Scott 2023-03-04 10:54:12 -05:00 committed by GitHub
commit 062cb032c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -165,19 +165,24 @@ def dashboard():
history_number = History.query.count() history_number = History.query.count()
history = History.query.order_by(History.created_on.desc()).limit(4).all() history = History.query.order_by(History.created_on.desc()).limit(4).all()
elif Setting().get('allow_user_view_history'): elif Setting().get('allow_user_view_history'):
history = db.session.query(History) \ allowed_domain_id_subquery = db.session.query(Domain.id) \
.join(Domain, History.domain_id == Domain.id) \
.outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \
.outerjoin(Account, Domain.account_id == Account.id) \ .outerjoin(Account, Domain.account_id == Account.id) \
.outerjoin(AccountUser, Account.id == AccountUser.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, DomainUser.user_id == current_user.id,
AccountUser.user_id == current_user.id AccountUser.user_id == current_user.id
)).all() )) \
history_number = len(history) # history.count() .subquery()
history = history[:4] 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) \
.filter(History.domain_id.in_(allowed_domain_id_subquery)) \
.count()
domain_count = db.session.query(Domain) \ domain_count = db.session.query(Domain) \
.outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \ .outerjoin(DomainUser, Domain.id == DomainUser.domain_id) \
.outerjoin(Account, Domain.account_id == Account.id) \ .outerjoin(Account, Domain.account_id == Account.id) \