# HG changeset patch # User Mads Kiilerich # Date 2015-01-06 00:54:36 # Node ID ad374c1716566be93bc7f224bcbc24c56bd0538a # Parent d8ad71e7b90d9047b01ae9accb50b9eb59f5534d comments: introduce lazy join of ChangesetStatus on ChangesetComment 99% of all uses of ChangesetComment will also need the optional corresponding ChangesetStatus (if any) on the status_change relationship. Fetching it on demand gives a lot of roundtrips and might be slow ... but adding explicit bulk queries everywhere do not seem feasible. Adding lazy=joined in the data model seems like the best solution. Loading a pull request with 10 comments on a slow repo goes from 3.5 s to 2.7 s. diff --git a/kallithea/model/db.py b/kallithea/model/db.py --- a/kallithea/model/db.py +++ b/kallithea/model/db.py @@ -2149,7 +2149,10 @@ class ChangesetComment(Base, BaseModel): author = relationship('User', lazy='joined') repo = relationship('Repository') - status_change = relationship('ChangesetStatus', cascade="all, delete-orphan") + # status_change is frequently used directly in templates - make it a lazy + # join to avoid fetching each related ChangesetStatus on demand. + # There will only be one ChangesetStatus referencing each comment so the join will not explode. + status_change = relationship('ChangesetStatus', cascade="all, delete-orphan", lazy='joined') pull_request = relationship('PullRequest') @classmethod