perf: eagerly load auxiliary data

This commit is contained in:
Lephe 2022-05-12 20:45:00 +01:00
parent b7dc2ebbf2
commit 8393cf1933
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 19 additions and 14 deletions

View File

@ -18,7 +18,8 @@ class Attachment(db.Model):
# The comment linked with
comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'),
nullable=False, index=True)
comment = db.relationship('Comment', backref=backref('attachments'))
comment = db.relationship('Comment', back_populates='attachments',
foreign_keys=comment_id)
# The size of the file
size = db.Column(db.Integer)

View File

@ -20,7 +20,8 @@ class Comment(Post):
backref=backref('comments', lazy='dynamic'),
foreign_keys=thread_id)
# attachments (relation from Attachment)
attachments = db.relationship('Attachment', back_populates='comment',
lazy='joined')
@property
def is_top_comment(self):

View File

@ -17,7 +17,7 @@ class SpecialPrivilege(db.Model):
# Member that is granted the privilege
member_id = db.Column(db.Integer, db.ForeignKey('member.id'), index=True)
member = db.relationship('Member', backref="special_privs",
member = db.relationship('Member', back_populates="special_privs",
foreign_keys=member_id)
# Privilege name
priv = db.Column(db.String(64))
@ -45,10 +45,9 @@ class Group(db.Model):
description = db.Column(db.UnicodeText)
# List of members (lambda delays evaluation)
members = db.relationship('Member', secondary=lambda: GroupMember,
back_populates='groups')
# Other fields populated automatically through relations:
# <privileges> list of privileges
back_populates='groups', lazy='joined')
# List of privileges
privileges = db.relationship('GroupPrivilege', back_populates='group')
def __init__(self, name, css, descr):
self.name = name
@ -87,7 +86,7 @@ class GroupPrivilege(db.Model):
id = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
group = db.relationship('Group', backref='privileges',
group = db.relationship('Group', back_populates='privileges',
foreign_keys=group_id)
priv = db.Column(db.String(64))

View File

@ -92,7 +92,7 @@ class Member(User):
# Groups and related privileges
groups = db.relationship('Group', secondary=GroupMember,
back_populates='members')
back_populates='members', lazy='joined')
# Personal information, all optional
bio = db.Column(db.UnicodeText)
@ -112,6 +112,15 @@ class Member(User):
trophies = db.relationship('Trophy', secondary=TrophyMember,
back_populates='owners')
# Specially-offered privileges (use self.special_privileges())
special_privs = db.relationship('SpecialPrivilege',
back_populates='member', lazy='joined')
# Other fields populated automatically through relations:
# <notifications> List of unseen notifications (of type Notification)
# <polls> Polls created by the member (of class Poll)
# Access to polymorphic posts
# TODO: Check that the query uses the double index on Post.{author_id,type}
def comments(self):
@ -138,11 +147,6 @@ class Member(User):
return int(level), int(level * 100) % 100
# Other fields populated automatically through relations:
# <special_privs> List of special privileges
# <notifications> List of unseen notifications (of type Notification)
# <polls> Polls created by the member (of class Poll)
def __init__(self, name, email, password):
"""Register a new user."""
self.name = name