Skip to content

perf: optimization for homepage referral activity tracking#991

Open
ankushchk wants to merge 3 commits intoalphaonelabs:mainfrom
ankushchk:perf/homepage-n-plus-one
Open

perf: optimization for homepage referral activity tracking#991
ankushchk wants to merge 3 commits intoalphaonelabs:mainfrom
ankushchk:perf/homepage-n-plus-one

Conversation

@ankushchk
Copy link

@ankushchk ankushchk commented Mar 1, 2026

What does this PR do?

The previous implementation of the index view ran a separate database query for every single top referrer to calculate their click counts. This is a classic N+1 query problem.

Furthermore, because these queries used path__contains (a SQL LIKE scan) on the WebRequest table which is not indexed for text containment and grows rapidly, the homepage load time would degrade exponentially as the site traffic increased.

Fix:

  1. Replaced the per-referrer loop with a single batched query using a combined Q expression.
  2. Resolved exact counts in-memory using Python.
  3. This reduces the number of database queries by up to 10x on every homepage load and avoids repeated expensive table scans.

Checklist

  • Did you run the pre-commit?
  • Did you test the change? (Verified referral click counts match correctly locally)
  • [/] Added screenshots to the PR description (not applicable - performance improvement)

Summary by CodeRabbit

  • Refactor
    • Optimized homepage performance by streamlining database queries: consolidated multiple referrer lookup operations into a single batch query and moved click count calculations to in-memory processing, reducing database load and improving page responsiveness.

@github-actions github-actions bot added the files-changed: 1 PR changes 1 file label Mar 1, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Mar 1, 2026

👀 Peer Review Required

Hi @ankushchk! This pull request does not yet have a peer review.

Before this PR can be merged, please request a review from one of your peers:

  • Go to the PR page and click "Reviewers" on the right sidebar.
  • Select a team member or contributor to review your changes.
  • Once they approve, this reminder will be automatically removed.

Thank you for contributing! 🎉

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 1, 2026

Warning

Rate limit exceeded

@ankushchk has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 41 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 644e45f and a33f917.

📒 Files selected for processing (1)
  • web/views.py

Walkthrough

Query performance optimization in the homepage view that consolidates N separate click-count queries into a single batched database query, then computes click totals in memory rather than issuing individual LIKE-scans per referral code.

Changes

Cohort / File(s) Summary
Query Batching Optimization
web/views.py
Replaced per-referrer click-count queries (N queries in a loop) with single batched query; builds in-memory mapping of click counts per referral code instead of issuing separate queries per ref code.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: a performance optimization for referral activity tracking on the homepage by addressing an N+1 query issue.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@web/views.py`:
- Around line 294-309: The current substring matching of referral codes
(variables all_codes, combined_q, matching_paths, click_counts) misattributes
clicks for codes that share prefixes and includes empty codes; fix by first
filtering out empty/None codes from all_codes, then build exact-match conditions
using path boundaries (e.g., require "/ref/{code}/" or "?ref={code}" followed by
end-of-string or "&") or use a regex-based Q (path__regex) that enforces
whole-code boundaries so "ABC" won't match "ABC1"; remove the fragile
first-match break and instead rely on the precise match to count each path to
the correct code when populating click_counts.

ℹ️ Review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e694ce8 and 644e45f.

📒 Files selected for processing (1)
  • web/views.py

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

files-changed: 1 PR changes 1 file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant