Skip to content

perf(boards): batch the owner lookup in admin board listings - #9395

Merged
lstein merged 4 commits into
invoke-ai:mainfrom
lstein:perf/board-listing-batched-queries
Aug 9, 2026
Merged

perf(boards): batch the owner lookup in admin board listings#9395
lstein merged 4 commits into
invoke-ai:mainfrom
lstein:perf/board-listing-batched-queries

Conversation

@lstein

@lstein lstein commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-on to #9163 (deferred non-merge-blocker).

The original finding was "board listing runs ~5 queries per board". Four of those five were already fixed inside #9163gallery.get_board_media_summaries now folds the cover-image, cover-video, image, asset and video-count queries into one windowed query for the whole page. This PR closes the residual: the per-board owner lookup.

BoardService.get_many / get_all called users.get(record.user_id) inside the DTO loop whenever the caller is an admin, so an admin listing 50 boards issued 50 extra single-row queries — for what is typically a handful of distinct owners.

Changes

  • UserService.get_many(user_ids) -> dict[str, UserDTO]: one IN (...) query, input deduped, chunked at 900 ids so a caller with many distinct owners can't exceed SQLite's bound-parameter limit (999 on builds predating 3.32). Unknown ids are simply absent from the result, so callers keep the None handling they already had.
  • BoardService._to_dtos: the two identical DTO-building loops in get_many/get_all collapse into one helper that fetches summaries and owner names once per page.
  • Non-admin listings don't display owner names, so they now skip the lookup entirely rather than branching on is_admin per board.

No API or schema change — owner_username is populated exactly as before.

Testing

  • New: test_admin_board_listing_batches_owner_lookup (20 boards / 3 distinct owners → one get_many, zero get), test_non_admin_board_listing_skips_owner_lookup, and three UserService.get_many tests (dedup + unknown ids, empty input, chunking past the parameter limit).
  • pytest tests/app/routers tests/app/services — 1212 passed, 3 skipped.

🤖 Generated with Claude Code

`BoardService.get_many` / `get_all` ran one `users.get` per board to resolve
the owner display name shown to admins — 50 boards meant 50 extra queries for
what is usually a handful of distinct owners. Adds `UserService.get_many`
(one `IN (...)` query, deduped and chunked under SQLite's bound-parameter
limit) and folds the two duplicated DTO-building loops into a single
`_to_dtos` helper that fetches media summaries and owner names once per page.

Non-admin listings never show owner names, so they now skip the lookup
entirely instead of relying on `is_admin` inside the loop.

The other 4-queries-per-board half of this finding was already fixed in invoke-ai#9163
itself (`gallery.get_board_media_summaries` batches covers and counts into one
windowed query); this is the residual.

Deferred non-blocker from PR invoke-ai#9163.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added python PRs that change python files services PRs that change app services python-tests PRs that change python tests labels Jul 28, 2026
@lstein lstein added the 6.14.1 label Jul 28, 2026
@lstein lstein moved this to 6.14.1: Bug fixes to 6.14.0 in Invoke - Community Roadmap Jul 28, 2026

@JPPhoto JPPhoto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

@lstein
lstein enabled auto-merge (squash) August 9, 2026 17:08
@lstein
lstein merged commit 45ad17b into invoke-ai:main Aug 9, 2026
15 checks passed
@lstein
lstein deleted the perf/board-listing-batched-queries branch August 9, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.14.1 python PRs that change python files python-tests PRs that change python tests services PRs that change app services

Projects

Status: 6.14.1: Bug fixes to 6.14.0

Development

Successfully merging this pull request may close these issues.

2 participants