Skip to content

fix: eager load Position and TelegramUser to eliminate N+1 queries in…#153

Open
chuks68 wants to merge 1 commit into
Quantarq:mainfrom
chuks68:fix/eager-load-notifications
Open

fix: eager load Position and TelegramUser to eliminate N+1 queries in…#153
chuks68 wants to merge 1 commit into
Quantarq:mainfrom
chuks68:fix/eager-load-notifications

Conversation

@chuks68

@chuks68 chuks68 commented Jun 20, 2026

Copy link
Copy Markdown

this pr closes #65 This PR resolves the N+1 query issue in UserDBConnector.get_users_for_notifications by eager loading the related Position and TelegramUser entities using SQLAlchemy's joinedload.
Configured positions and telegram_user relationships on the User model.
Updated the query in get_users_for_notifications to apply .options(joinedload(User.positions), joinedload(User.telegram_user)).
Re-mapped query outputs to return the expected (contract_address, telegram_id) tuple type to preserve the existing signature and behavior.

@YaronZaki YaronZaki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the joinedload fix — clean way to kill the N+1. CI is failing on the test job though, so please address that and push a fix before we can land this.

Copy link
Copy Markdown
Contributor

Hi @chuks68 — eager loading User.positions and User.telegram_user is the right call, and the explicit relationship definitions make this cleaner long-term. CI is currently failing on foreign-key constraint violations in the test suite:

insert or update on table "airdrop" violates foreign key constraint "airdrop_user_id_fkey"
insert or update on table "transaction" violates foreign key constraint "transaction_position_id_fkey"

This looks like a fixture-ordering issue (children being inserted before their parent user/position records), unrelated to the eager-loading change itself but exposed by it. Could you re-check the fixture setup in the affected tests so parents are seeded first? Once CI is green, easy merge. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: Fix N+1 queries in get_users_for_notifications() with eager loading

2 participants