Skip to content

Commit f69fb12

Browse files
committed
Improve user in_guild sync query time by only loading the id column
This means the other, larger, columns do not need to be deserialised, bringing the query time down from 7.4s down to 3.5s. I couldn't simply do select(models.User.id) here, as we need the full User object in order to mutate and update it later inthe process.
1 parent 7898288 commit f69fb12

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

metricity/exts/event_listeners/startup_sync.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pydis_core.utils import logging, scheduling
88
from sqlalchemy import column, select
99
from sqlalchemy.dialects.postgresql import insert
10+
from sqlalchemy.orm import load_only
1011

1112
from metricity import models
1213
from metricity.bot import Bot
@@ -95,7 +96,9 @@ async def sync_guild(self) -> None:
9596
users_updated = 0
9697
guild_member_ids = {str(member.id) for member in guild.members}
9798
async with async_session() as sess:
98-
res = await sess.execute(select(models.User).filter_by(in_guild=True))
99+
100+
stmt = select(models.User).filter_by(in_guild=True).options(load_only(models.User.id))
101+
res = await sess.execute(stmt)
99102
in_guild_users = res.scalars()
100103
for user in in_guild_users:
101104
if user.id not in guild_member_ids:

0 commit comments

Comments
 (0)