Dashboard history query performance related change to nested subquery (allowed domains for user).

This commit is contained in:
Rauno Tuul 2023-03-01 11:42:21 +02:00
parent dc69f00094
commit ec687b13a5

View File

@ -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) \