Skip to content

⚡ perf: optimize webhook deduplication by eliminating N+1 query#67

Open
Sparkier wants to merge 1 commit into
mainfrom
perf-webhook-n1-query-5224960296347097962
Open

⚡ perf: optimize webhook deduplication by eliminating N+1 query#67
Sparkier wants to merge 1 commit into
mainfrom
perf-webhook-n1-query-5224960296347097962

Conversation

@Sparkier

Copy link
Copy Markdown
Owner

💡 What

Modified the Google Sheets webhook POST handler to perform a single batched read (.in('title', titles).in('contact', contacts)) instead of N individual .maybeSingle() queries. Uses an in-memory Set for O(1) deduplication logic across the entire payload batch, and tracks newly inserted posts live within the batch loop to mirror the original functionality perfectly.

🎯 Why

The original code contained an N+1 query loop when reading duplicate-check data, where N is the number of rows inserted into the webhook payload. For larger payloads, this generated linear delays due to excessive round-trips to Supabase, slowing down processing dramatically.

📊 Measured Improvement

I wrote a perf.test.ts benchmark targeting the /api/webhooks/google-sheets endpoint to measure the difference with 2,000 synthetic rows of data.

  • Baseline / Improvement: Originally, evaluating 2,000 rows caused 2,000 calls to maybeSingle. After optimization, it drops to 1 large .in() query (an execution count reduction from 2,000 to 1). The test runtime executes almost identically in mock (~160ms) because it relies on mock overhead, but in a real-world scenario over network I/O, eliminating 1,999 database queries directly resolves severe performance degradation on large payload synchronization.

PR created automatically by Jules for task 5224960296347097962 started by @Sparkier

Pre-fetches existing posts in a single query based on the incoming payload instead of firing individual 'maybeSingle' queries for each iteration. Creates a Set to handle O(1) lookups during the deduplication phase. Also updates the Set progressively to catch duplicate entries in the same payload batch. Added performance benchmarks verifying the query load drop.

Co-authored-by: Sparkier <5690524+Sparkier@users.noreply.github.com>
@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.

@vercel

vercel Bot commented Jul 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
vis-positions Ready Ready Preview, Comment Jul 18, 2026 3:12pm

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