Skip to content

Fix N+1 query performance issues in stats and list endpoints - #1161

Merged
Tim020 merged 1 commit into
devfrom
feature/performance-n1-query-fixes
Jun 7, 2026
Merged

Fix N+1 query performance issues in stats and list endpoints#1161
Tim020 merged 1 commit into
devfrom
feature/performance-n1-query-fixes

Conversation

@Tim020

@Tim020 Tim020 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • cast/stats and character/stats were firing 5,137 individual SQL queries per request (~8 seconds each), blocking Tornado's event loop for the duration and queueing all other requests behind them. Root cause: a Python loop over revision.line_associations with lazy-loaded access at every level (association → line → line_parts → cuts per-part → character per-part → group.characters per-group). Fixed by replacing session.get(ScriptRevision, ...) with a select(...).options(selectinload(...)) chain that loads all required data in ~8 bulk queries, plus a single upfront set-query for revision-scoped cut IDs replacing per-part lazy loads.
  • GET /show/character had an N+1 per character for character_groups and mic_allocations during schema serialisation. Fixed by adding selectinload options to the character query in CharacterController.
  • GET /show/act and GET /show/scene were loading ~950 script line ORM objects per act/scene via ActSchema/SceneSchema with include_relationships = True. The lines relationship is confirmed unused by both frontend clients (neither Act nor Scene TypeScript interface includes a lines field). Fixed by adding exclude = ("lines",) to both schemas.
  • Adds 11 new behavioural count-verification tests for cast/stats and character/stats covering: dialogue lines counted, characters without cast excluded, cut lines excluded, stage directions excluded, character group expansion, and multi-act/scene attribution.

Test plan

  • Run pytest test/controllers/api/v1/show/test_cast.py test/controllers/api/v1/show/test_characters.py -v — all 26 tests should pass
  • Run full pytest suite — all 662 tests should pass
  • Manually load the DigiScript UI and navigate to the cast/character stats pages — verify they load quickly (seconds → milliseconds)
  • Verify act and scene list pages still display correctly (no missing data from the schema exclusion)

🤖 Generated with Claude Code

cast/stats and character/stats were firing 5,137 individual SQL queries
per request (~8s) due to nested lazy-loaded ORM traversal. Replace with
selectinload chains that load all required data in ~8 bulk queries, and
a single upfront set-query for revision-scoped cuts rather than one
check per line part.

Also fixes character list N+1 (per-character character_group and
mic_allocation lazy loads) via selectinload on CharacterController, and
removes Act.lines / Scene.lines from their schemas — both relationships
loaded ~950 ORM rows per act/scene on every list request but were never
read by either frontend client.

Adds 11 behavioural count-verification tests for cast/stats and
character/stats to document and guard the exact counting semantics.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Tim020 Tim020 added the claude Issues created by Claude label Jun 7, 2026
@github-actions github-actions Bot added large-diff server Pull requests changing back end code labels Jun 7, 2026
@github-actions

github-actions Bot commented Jun 7, 2026

Copy link
Copy Markdown

Client V3 Test Results

23 tests   23 ✅  0s ⏱️
 2 suites   0 💤
 1 files     0 ❌

Results for commit 3c22e32.

@github-actions

github-actions Bot commented Jun 7, 2026

Copy link
Copy Markdown

Client Test Results

128 tests   128 ✅  0s ⏱️
  6 suites    0 💤
  1 files      0 ❌

Results for commit 3c22e32.

@sonarqubecloud

sonarqubecloud Bot commented Jun 7, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
48.1% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@github-actions

github-actions Bot commented Jun 7, 2026

Copy link
Copy Markdown

Python Test Results

  1 files    1 suites   2m 10s ⏱️
662 tests 662 ✅ 0 💤 0 ❌
667 runs  667 ✅ 0 💤 0 ❌

Results for commit 3c22e32.

@github-actions

github-actions Bot commented Jun 7, 2026

Copy link
Copy Markdown

Playwright E2E Results (chromium)

164 tests   164 ✅  1m 39s ⏱️
 14 suites    0 💤
  1 files      0 ❌

Results for commit 3c22e32.

@github-actions

github-actions Bot commented Jun 7, 2026

Copy link
Copy Markdown

Playwright E2E Results (firefox)

164 tests   164 ✅  1m 44s ⏱️
 14 suites    0 💤
  1 files      0 ❌

Results for commit 3c22e32.

@Tim020
Tim020 merged commit 007429c into dev Jun 7, 2026
34 of 35 checks passed
@Tim020
Tim020 deleted the feature/performance-n1-query-fixes branch June 7, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude Issues created by Claude large-diff server Pull requests changing back end code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant