Skip to content

⚡ Fix N+1 Query in Email Scanner Deduplication#77

Merged
5queezer merged 3 commits intomainfrom
fix/email-scanner-n-plus-one-16840684933250202831
Mar 20, 2026
Merged

⚡ Fix N+1 Query in Email Scanner Deduplication#77
5queezer merged 3 commits intomainfrom
fix/email-scanner-n-plus-one-16840684933250202831

Conversation

@5queezer
Copy link
Copy Markdown
Owner

💡 What:
The loop in scanUserInbox that deduplicates newly fetched messages against the database has been refactored. Instead of executing a database query for each individual message to check if it exists (prisma.scannedEmail.findUnique), it now extracts all message IDs upfront. It performs a single bulk query (prisma.scannedEmail.findMany with an in clause) to fetch matching message IDs, stores them in a Set, and uses this Set for fast O(1) lookups during the loop.

🎯 Why:
The previous implementation suffered from an N+1 query issue. If 50 messages were fetched, it resulted in 50 distinct database queries. This significantly increased database load and execution time due to network latency overhead for each query. This change ensures that only one database query is executed, regardless of the number of messages processed.

📊 Measured Improvement:
While I could not establish a strict execution baseline because the scanner connects to a live Gmail API via an external service which requires a valid OAuth integration, we know conceptually that this is a drastic improvement. A single query with an IN clause replaces N distinct queries, completely eliminating the network round-trip latency associated with each additional findUnique call. This reduces the time complexity of the I/O operation from O(N) to O(1) (with respect to number of messages), greatly enhancing processing speed and reducing database load.


PR created automatically by Jules for task 16840684933250202831 started by @5queezer

@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@5queezer
Copy link
Copy Markdown
Owner Author

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 88bef19135

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/email/scanner.ts
Comment on lines +43 to +45
const existingScans = await prisma.scannedEmail.findMany({
where: {
userId,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid stale prefetches for message deduplication

Because scanUserInbox() can be invoked concurrently from both the scheduled and manual /api/email/scan paths (app/api/email/scan/route.ts:18-30), this findMany() result is only a snapshot taken before the loop starts. If another in-flight scan inserts a row after this query—or if fetchNewMessages() returns the same Gmail ID twice, since it currently flattens messagesAdded without deduping (lib/email/gmail.ts:113-117)—existingMessageIds stays stale and the later scannedEmail.create() turns what used to be a skipped duplicate into a @@unique([userId, messageId]) violation (prisma/schema.prisma:177).

Useful? React with 👍 / 👎.

@5queezer
Copy link
Copy Markdown
Owner Author

@google-labs-jules review

@google-labs-jules
Copy link
Copy Markdown
Contributor

@google-labs-jules review

Understood. Performing code review...

google-labs-jules Bot and others added 3 commits March 20, 2026 02:24
- Pre-fetch all existing scanned emails using an `in` query.
- Use a `Set` for O(1) existence checks.
- This eliminates the N+1 database queries performed when iterating over fetched messages.

Co-authored-by: 5queezer <34570565+5queezer@users.noreply.github.com>
- Pre-fetch all existing scanned emails using an `in` query.
- Use a `Set` for O(1) existence checks.
- This eliminates the N+1 database queries performed when iterating over fetched messages.

Co-authored-by: 5queezer <34570565+5queezer@users.noreply.github.com>
- Pre-fetch all existing scanned emails using an `in` query.
- Use a `Set` for O(1) existence checks.
- This eliminates the N+1 database queries performed when iterating over fetched messages.

Co-authored-by: 5queezer <34570565+5queezer@users.noreply.github.com>
@5queezer 5queezer force-pushed the fix/email-scanner-n-plus-one-16840684933250202831 branch from 8072ef9 to 6190d76 Compare March 20, 2026 01:24
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@5queezer 5queezer merged commit 7cde93c into main Mar 20, 2026
2 checks passed
@5queezer 5queezer deleted the fix/email-scanner-n-plus-one-16840684933250202831 branch March 20, 2026 01:28
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