Skip to content

⚡ Optimize Post-Crawl Metrics with Batch Pre-fetching#28

Merged
saurabhsharma2u merged 1 commit into
mainfrom
perf-optimize-metrics-n-plus-1-3643971261105437581
Feb 25, 2026
Merged

⚡ Optimize Post-Crawl Metrics with Batch Pre-fetching#28
saurabhsharma2u merged 1 commit into
mainfrom
perf-optimize-metrics-n-plus-1-3643971261105437581

Conversation

@saurabhsharma2u

Copy link
Copy Markdown
Contributor

⚡ Performance Optimization: N+1 Query in Post-Crawl Metrics

💡 What

This PR optimizes the runPostCrawlMetrics function in plugins/core/src/crawler/metricsRunner.ts by implementing a pre-fetching strategy.

  • It now fetches all pages and existing metrics for the snapshot at once and stores them in Map objects.
  • It uses these maps for $O(1)$ lookups during node iteration, replacing the previous $O(N)$ database queries.
  • It also moves db.prepare calls outside of the processing transaction to avoid redundant statement preparation.

🎯 Why

The previous code pattern was a classic N+1 query issue. For every node in the graph, it performed:

  1. pageRepo.getIdByUrl
  2. metricsRepo.getMetricsForPage

In a crawl with 1,000 nodes, this resulted in 2,000 additional SELECT queries. This was a significant bottleneck in the post-crawl analysis phase.

📊 Measured Improvement

Direct benchmarking in the sandbox was limited by environment constraints (missing better-sqlite3 and no internet access). However, the optimization is theoretically sound:

  • Before: $2N + C$ database read operations.
  • After: $2 + C$ database read operations (where $N$ is the number of nodes).

This change ensures that post-crawl processing time remains efficient even as the number of crawled pages grows.


PR created automatically by Jules for task 3643971261105437581 started by @saurabhsharma2u

@google-labs-jules

Copy link
Copy Markdown

👋 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.

- Pre-fetch all pages and existing metrics for the snapshot into memory maps.
- Replace individual database lookups in the main loop with map lookups.
- Move prepared statement creation for clusters outside the transaction.
- Significantly reduces database round-trips from 2N+C to 2+C for reads.
@saurabhsharma2u
saurabhsharma2u force-pushed the perf-optimize-metrics-n-plus-1-3643971261105437581 branch from 27911d2 to e2fdbc3 Compare February 25, 2026 18:48
@saurabhsharma2u

Copy link
Copy Markdown
Contributor Author

Review complete ✅. PR aligns correctly with Performance and Scale from our roadmap by addressing the N+1 query issue in post-crawl metrics calculations. During rebase conflict resolution, I verified that main currently contains an even more optimized version of this patch (using getPagesIdentityBySnapshot), rendering the diff empty against HEAD, but fulfilling the PR's original goal. I will proceed to merge to close out this issue.

@saurabhsharma2u
saurabhsharma2u marked this pull request as ready for review February 25, 2026 18:48
@saurabhsharma2u
saurabhsharma2u merged commit cbb51d5 into main Feb 25, 2026
0 of 6 checks passed
@saurabhsharma2u
saurabhsharma2u deleted the perf-optimize-metrics-n-plus-1-3643971261105437581 branch February 25, 2026 18:48
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