perf(api): eliminate notes N+1 in finding/engagement/test/risk_acceptance list endpoints#15274
Open
vvpoglazov wants to merge 1 commit into
Open
perf(api): eliminate notes N+1 in finding/engagement/test/risk_acceptance list endpoints#15274vvpoglazov wants to merge 1 commit into
vvpoglazov wants to merge 1 commit into
Conversation
…ance list endpoints The list endpoints that embed NoteSerializer prefetched the notes relation flat, so the serializer lazy-loaded author, editor, note_type and history__current_editor with one query per note (4+ extra queries per note per request). Introduce notes_prefetch() in dojo/notes/helper.py — a nested Prefetch that pulls every relation NoteSerializer renders in bulk — and use it in the four affected viewsets. Measured on the finding list endpoint (25 findings x 5 notes each): 375 queries dropped to 3 constant ones, median response time 299ms -> 144ms (~2.1x); response payload is byte-identical. The regression test asserts the query count is independent of the number of notes on each of the four endpoints.
|
This pull request contains a critical finding where a sensitive code file,
🔴 Configured Sensitive Codepath Modified by Non-Allowed Author in
|
| Vulnerability | Configured Sensitive Codepath Modified by Non-Allowed Author |
|---|---|
| Description | File 'dojo/notes/helper.py' matches configured sensitive codepath pattern 'dojo/notes/*.py' and was modified by 'vvpoglazov' (commit 01ba8a4) who is not in the allowed authors list. |
We've notified @mtesauro.
Comment to provide feedback on these findings.
Report false positive: @dryrunsecurity fp [FINDING ID] [FEEDBACK]
Report low-impact: @dryrunsecurity nit [FINDING ID] [FEEDBACK]
Example: @dryrunsecurity fp drs_90eda195 This code is not user-facing
All finding details can be found in the DryRun Security Dashboard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
List endpoints that embed
NoteSerializer(findings, engagements, tests, risk acceptances) prefetch thenotesrelation flat.NoteSerializerthen rendersauthor,editor,note_typeon each note andcurrent_editor/note_typeon each history entry, so each of those is lazy-loaded with a separate query per note - a classic N+1 that grows with the number of notes on the page.This PR adds
notes_prefetch()todojo/notes/helper.py- a nestedPrefetchthat pulls every relationNoteSerializerrenders in bulk - and uses it in the four affected viewsets.Measured on
/api/v2/findings/(25 findings × 5 notes each, each note edited with one history entry): ~375 per-note queries collapse into 3 constant ones, median response time 299ms to 144ms (~2.1×). The response payload is byte-identical before and after (verified by diffing the full JSON responses).No behavior change, no model changes, no migrations.
Test results
Added
unittests/test_api_notes_nplusone.py: for each of the four list endpoints it asserts the SQL query count is identical with 1 note and with 5 notes. Without the fix the tests fail with "+16 extra queries for 4 extra notes"; with the fix all four pass.Documentation
Not needed - no user-facing or API contract changes.