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

@ -130,8 +130,12 @@ def seed_db():
db.session.add(comment)
comments.append(comment)
db.session.commit()
db.session.flush()
for c in comments:
c.top_comment_id = c.id
db.session.add(c)
db.session.commit()
print(f"Creating {NUM_REPLY_COMMENTS} reply comments")
for i in range(NUM_REPLY_COMMENTS):
@ -143,6 +147,7 @@ def seed_db():
author_id=user.id,
parent_submission=str(parent.post.id),
parent_comment_id=parent.id,
top_comment_id=parent.top_comment_id,
level=parent.level + 1,
over_18=False,
is_bot=False,
@ -155,7 +160,6 @@ def seed_db():
comments.append(comment)
db.session.commit()
db.session.flush()
print("Updating comment counts for all posts")
for post in posts: