Eager load comments for post rendering.

GET /post/1/clever-unique-post-title-number-0
|----------|--------|--------|--------|--------|--------|------------|
| Database | SELECT | INSERT | UPDATE | DELETE | Totals | Duplicates |
|----------|--------|--------|--------|--------|--------|------------|
| default  |  942   |   0    |   1    |   0    |  943   |    921     |
|----------|--------|--------|--------|--------|--------|------------|
Total queries: 943 in 0.377s # request time in browser 17249ms

GET /post/1/clever-unique-post-title-number-0
|----------|--------|--------|--------|--------|--------|------------|
| Database | SELECT | INSERT | UPDATE | DELETE | Totals | Duplicates |
|----------|--------|--------|--------|--------|--------|------------|
| default  |   58   |   0    |   1    |   0    |   59   |     35     |
|----------|--------|--------|--------|--------|--------|------------|
Total queries: 59 in 0.0423s # request time in browser 544ms

Also, fixes seed_db not populating top_comment_id on generated
comments. If you want to test locally with seed_db test data, you need
to reseed.
This commit is contained in:
TLSM 2022-11-28 17:47:54 -05:00
parent 5aaef144cf
commit afe209d5d8
No known key found for this signature in database
GPG key ID: E745A82778055C7E
5 changed files with 125 additions and 8 deletions

View file

@ -58,8 +58,13 @@ class Comment(Base):
senttouser = relationship("User", primaryjoin="User.id==Comment.sentto", viewonly=True)
parent_comment = relationship("Comment", remote_side=[id], viewonly=True)
child_comments = relationship("Comment", lazy="dynamic", remote_side=[parent_comment_id], viewonly=True)
awards = relationship("AwardRelationship", viewonly=True)
reports = relationship("CommentFlag", viewonly=True)
awards = relationship("AwardRelationship",
primaryjoin="AwardRelationship.comment_id == Comment.id",
viewonly=True)
reports = relationship("CommentFlag",
primaryjoin="CommentFlag.comment_id == Comment.id",
order_by="CommentFlag.created_utc",
viewonly=True)
notes = relationship("UserNote", back_populates="comment")
def __init__(self, *args, **kwargs):
@ -70,7 +75,6 @@ class Comment(Base):
super().__init__(*args, **kwargs)
def __repr__(self):
return f"<Comment(id={self.id})>"
@property
@ -87,7 +91,7 @@ class Comment(Base):
@lazy
def flags(self, v):
flags = g.db.query(CommentFlag).filter_by(comment_id=self.id).order_by(CommentFlag.created_utc).all()
flags = self.reports
if not (v and (v.shadowbanned or v.admin_level > 2)):
for flag in flags:
if flag.user.shadowbanned: