Skip to content

perf: fix N+1 queries on task assignees and news entity names#1044

Merged
nledez merged 3 commits intocgwire:mainfrom
nledez:perf/fix-n-plus-1-assignees-and-entity-names
Apr 7, 2026
Merged

perf: fix N+1 queries on task assignees and news entity names#1044
nledez merged 3 commits intocgwire:mainfrom
nledez:perf/fix-n-plus-1-assignees-and-entity-names

Conversation

@nledez
Copy link
Copy Markdown
Contributor

@nledez nledez commented Apr 3, 2026

Problem

Two N+1 query patterns cause unnecessary database load:

  1. get_full_task() calls get_person() individually for each assignee, resulting in N queries for N assignees.
  2. get_last_news_for_project() (used by get_news_for_entity()) calls get_full_entity_name() in a loop, issuing up to 3 queries per news entry (entity + parent + grandparent).

Solution

  • Add get_persons_by_ids() in persons_service.py: fetches all assignees in a single WHERE id IN (...) query. Used in get_full_task() instead of the per-assignee loop.
  • Add get_full_entity_names() in names_service.py: batch version of get_full_entity_name() that pre-fetches entities, parents, and grandparents in 2-3 queries max regardless
    of result count. Used in get_last_news_for_project() instead of the per-news-entry call.

Both changes preserve the existing return formats and behavior.

nledez added 3 commits April 7, 2026 10:46
  - Add get_persons_by_ids() to batch-fetch assignees in a single query
  instead of one get_person() per assignee in get_full_task()
  - Add get_full_entity_names() to batch-fetch entities, parents, and
  grandparents in 2-3 queries instead of N calls to
  get_full_entity_name() in get_last_news_for_project()
  - Batch-fetch assets, types, projects, and previews in search_assets()
  - Batch-fetch shots, sequences, episodes, projects, and previews in search_shots()
  - Batch-fetch persons in search_persons()
  - Batch-fetch task statuses in _handle_hashtags()
@nledez nledez force-pushed the perf/fix-n-plus-1-assignees-and-entity-names branch from 91014cf to a10eb75 Compare April 7, 2026 10:04
@nledez nledez merged commit c631c25 into cgwire:main Apr 7, 2026
16 checks passed
@nledez nledez deleted the perf/fix-n-plus-1-assignees-and-entity-names branch April 7, 2026 10:19
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.

1 participant