Skip to content

Fix n+1 query issue in message deleteion context - #1634

Open
ChrisLovering wants to merge 2 commits into
mainfrom
fix-n+1-issue-in-message-deletion-context
Open

Fix n+1 query issue in message deleteion context#1634
ChrisLovering wants to merge 2 commits into
mainfrom
fix-n+1-issue-in-message-deletion-context

Conversation

@ChrisLovering

@ChrisLovering ChrisLovering commented Jul 9, 2026

Copy link
Copy Markdown
Member

Declare id and author as plain integers to drop DRF's per-row UniqueValidator and FK lookup, and validate all authors and IDs with one batched query each. Query count is now constant instead of 2 per message.

@ChrisLovering
ChrisLovering requested review from jb3 and jchristgit July 9, 2026 22:26
@netlify

netlify Bot commented Jul 9, 2026

Copy link
Copy Markdown

Deploy Preview for pydis-static ready!

Name Link
🔨 Latest commit 6ef6742
🔍 Latest deploy log https://app.netlify.com/projects/pydis-static/deploys/6a5021b8a5255e000894bd86
😎 Deploy Preview https://deploy-preview-1634--pydis-static.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Declare `id` and `author` as plain integers to drop DRF's per-row
UniqueValidator and FK lookup, and validate all authors and IDs with one
batched query each. Query count is now constant instead of 2 per message.
@ChrisLovering
ChrisLovering force-pushed the fix-n+1-issue-in-message-deletion-context branch from a71eb85 to 6ef6742 Compare July 9, 2026 22:33
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — fix-n+1-issue-in-message-deletion-context into main

@jb3

jb3 commented Jul 9, 2026

Copy link
Copy Markdown
Member

deferring to @jchristgit for a review 🧌

@jb3
jb3 removed their request for review July 9, 2026 23:49
@jchristgit jchristgit self-assigned this Jul 27, 2026

@jchristgit jchristgit 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.

Declare uid=502(chris) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),702(com.apple.sharepoint.group.2),100(_lpoperator),701(com.apple.sharepoint.group.1) and as plain integers to drop DRF's per-row UniqueValidator and FK lookup, and validate all authors and IDs with one batched query each. Query count is now constant instead of 2 per message.

I can't help but feel that this isn't a good solution, but it also appears that there is no better solution.

There is a ticket for DRF at encode/django-rest-framework#9607. I suppose this is a nice demonstration of where it falls short.

I guess n+1s are problematic from a performance perspective, but we're not exactly chasing top of the shelf response time here - does this solve an actual problem or just some noise in Sentry? Because if not, I'm inclined to say we leave it as is, because this code would be annoying to maintain. I would rather think about how to fix the issue in DRF or perhaps even how to stop using it altogether, but it's not like anyone has time for that anyways.

@jchristgit

Copy link
Copy Markdown
Contributor

FWIW, I also tried various solutions with select_related, but those don't have any effect, because we're trying to create something new here - those would probably help on the GET endpoints though.

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.

4 participants