Skip to content

perf(dora): optimize change lead time calculation using batch queries#8714

Merged
klesh merged 4 commits intoapache:mainfrom
AnkeshThakur:feat-optimize-change-lead-time-batch-queries
Feb 26, 2026
Merged

perf(dora): optimize change lead time calculation using batch queries#8714
klesh merged 4 commits intoapache:mainfrom
AnkeshThakur:feat-optimize-change-lead-time-batch-queries

Conversation

@AnkeshThakur
Copy link
Copy Markdown
Contributor

Summary

This PR optimizes the change lead time calculation in the DORA plugin by eliminating the N+1 query problem through batch fetching.

Changes

Performance Optimization

  • Implemented batchFetchFirstCommits() - fetches all first commits in a single query
  • Implemented batchFetchFirstReviews() - fetches all first reviews in a single query
  • Implemented batchFetchDeployments() - fetches all deployments in a single query
  • Modified CalculateChangeLeadTime() to call batch functions upfront and use O(1) map lookups

Bug Fix

  • Fixed NULL author_id handling in review queries to properly process PRs with empty author_id field

Performance Impact

Before optimization:

  • 3 database queries per PR
  • For 10,000 PRs: 30,001 queries

After optimization:

  • 3 batch queries total regardless of PR count
  • For 10,000 PRs: 3 queries (99.99% reduction)

Testing

  • ✅ All existing unit tests pass
  • ✅ All E2E tests pass including edge cases with NULL author_id
  • ✅ Batch fetch operations complete in ~5-6ms for test dataset
  • ✅ Data integrity verified - results match previous implementation

Related Issue

This addresses performance issues with change lead time calculation for repositories with large numbers of pull requests, where the previous implementation caused significant database load and slow calculation times.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. component/plugins This issue or PR relates to plugins improvement priority/high This issue is very important labels Feb 14, 2026
@AnkeshThakur AnkeshThakur marked this pull request as draft February 14, 2026 10:03
Eliminates N+1 query problem by implementing batch fetching for:
- First commits per PR (batchFetchFirstCommits)
- First reviews per PR (batchFetchFirstReviews)
- Deployments per project (batchFetchDeployments)

Performance improvement:
- Before: 3 queries per PR (30,001 queries for 10K PRs)
- After: 3 batch queries total (99.99% reduction)

Also fixes NULL author_id handling in review queries to properly
handle PRs with empty author_id field.

Tested with E2E tests confirming correctness and performance gains.

Signed-off-by: Ankesh <athakur@g2.com>
@AnkeshThakur AnkeshThakur force-pushed the feat-optimize-change-lead-time-batch-queries branch from 59ed07e to d536475 Compare February 14, 2026 10:05
@AnkeshThakur AnkeshThakur marked this pull request as ready for review February 17, 2026 05:50
@dosubot dosubot Bot added pr-type/bug-fix This PR fixes a bug severity/p1 This bug affects functionality or significantly affect ux labels Feb 17, 2026
Copy link
Copy Markdown
Contributor

@klesh klesh left a comment

Choose a reason for hiding this comment

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

Good job. Can you fix the linting error so I can merge it?
It is OK to delete the obsolete functions

…ueries

Signed-off-by: Ankesh <athakur@g2.com>
@AnkeshThakur AnkeshThakur requested a review from klesh February 23, 2026 18:20
Signed-off-by: Ankesh <athakur@g2.com>
Signed-off-by: Ankesh <athakur@g2.com>
Copy link
Copy Markdown
Contributor

@klesh klesh left a comment

Choose a reason for hiding this comment

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

LGTM. It's great to see this kind of improvement—thank you for your contribution!

@klesh klesh merged commit df683a6 into apache:main Feb 26, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/plugins This issue or PR relates to plugins improvement pr-type/bug-fix This PR fixes a bug priority/high This issue is very important severity/p1 This bug affects functionality or significantly affect ux size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants