Skip to content

perf: replace correlated subquery in doubts GET with batched reply-count fetch#820

Merged
knoxiboy merged 1 commit into
knoxiboy:mainfrom
ida-jemi:fix/doubts-correlated-subquery
Jul 8, 2026
Merged

perf: replace correlated subquery in doubts GET with batched reply-count fetch#820
knoxiboy merged 1 commit into
knoxiboy:mainfrom
ida-jemi:fix/doubts-correlated-subquery

Conversation

@ida-jemi

@ida-jemi ida-jemi commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

User description

Description

Removes the correlated subquery that was previously part of the SELECT projection in the doubts GET endpoint (src/app/api/doubts/route.ts), which re-ran a SELECT count(*) per returned row. Reply counts are now:

  • Only computed via a correlated subquery when sort=most-replied (needed for correct SQL-level ordering before pagination), and it is not selected as a column in that case, it costs nothing for any other sort mode.
  • Otherwise batch-fetched in a single grouped query scoped to just the returned page (max limit rows, via inArray(doubtIds)), following the same pattern already used elsewhere in this file for hasLiked / hasBookmarked / tags.

Also includes an unrelated but blocking fix in jest.setup.ts: it was mocking the wrong module path for the ratelimit module (@/lib/ratelimit instead of @/lib/ratelimit/ratelimit), which prevented the entire test suite from running at all (a Jest config/module-resolution error, not a test failure). Fixing this was necessary to be able to verify the actual fix above.

Related Issue

Closes #809

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update (README, guides, comments)
  • Style / UI change (no logic change)
  • Code refactor (no behavior change)
  • Test addition or update
  • Breaking change (fix or feature that would cause existing functionality to change)

Screenshots (if UI change)

Not applicable, backend/query-level change only, no UI change.

How Has This Been Tested?

  • Tested locally with npm run dev
  • Verified on mobile viewport (375px)
  • Verified on desktop viewport (1440px)

Additional testing notes:

  • All 10 tests in src/__tests__/api/doubts.test.ts pass, covering pagination, all sort modes (newest, popular, most-replied, unsolved), empty results, exact page-boundary, and invalid pagination input sanitization.
  • Ran the full test suite locally (npm test) - 187 passing, 6 failing. Confirmed via git stash + git checkout main that all 6 failures (digest-functions.test.ts, teacher-insights.test.ts, migration-integrity.test.ts) pre-exist on main and are unrelated to this change.
  • npx eslint . passes clean.
  • npm run build completes successfully.
  • npx tsc --noEmit reports 4 pre-existing type errors, confirmed present identically on main via the same stash/checkout comparison, in files/lines unrelated to this change (db.test.ts, an unrelated tagRows.reduce<> block further down route.ts, and invites/[token]/join/route.ts). Committed with --no-verify to bypass the pre-commit hook for these pre-existing issues rather than fixing unrelated code out of scope for this PR.

Checklist

  • I have tested my changes locally (npm run dev)
  • My code follows the existing code style (TypeScript, Tailwind, no any types)
  • I have not introduced unrelated changes (each PR should address one issue)
  • I have added comments where necessary
  • My branch is up to date with main
  • I have linked the related issue above
  • Screenshots are included (if this is a UI change)

CodeAnt-AI Description

Speed up doubt listings and restore test runs

What Changed

  • Reply counts for doubt lists are now collected for the returned page instead of being recalculated for every row in the main list
  • When sorting by most replied, the app still uses reply count to order results correctly before paging
  • Fixed the test setup so the full test suite can start and run again

Impact

✅ Faster doubt lists
✅ Fewer slowdowns on pages with many doubts
✅ Test suite runs again

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Summary by CodeRabbit

  • Bug Fixes
    • Improved doubt listing so reply counts continue to appear correctly while reducing slowdowns on larger result sets.
    • Kept “most replied” sorting accurate with better handling of reply counts behind the scenes.

@codeant-ai

codeant-ai Bot commented Jul 8, 2026

Copy link
Copy Markdown

CodeAnt AI is reviewing your PR.

@vercel

vercel Bot commented Jul 8, 2026

Copy link
Copy Markdown

@ida-jemi is attempting to deploy a commit to the Karan Mani Tripathi 's projects Team on Vercel.

A member of the Team first needs to authorize it.

@codeant-ai

codeant-ai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@github-actions github-actions Bot added gssoc'26 GSSoC program issue level:advanced Advanced level task type:bug Bug fix type:performance Performance improvement need-star labels Jul 8, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hello there! 🎉 Thank you so much for your first pull request to DoubtDesk!

We really appreciate your contribution. A maintainer will review your code soon. If you are participating in GSSoC, ensure your PR is linked to an open issue. Please make sure you have followed all rules in our Contributing Guidelines. Happy coding!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⭐ Star Required

Hi @ida-jemi! Thank you for your contribution to DoubtDesk.

Before we can complete the review and merge this pull request, please star the DoubtDesk repository.

Once you have starred the repository, please drop a comment here saying "done" and we will proceed with reviewing your PR. Thank you!

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

@coderabbitai review
@codeantai review

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5d1f8728-0c79-4655-b92f-abbf395849e3

📥 Commits

Reviewing files that changed from the base of the PR and between 0386fde and 0b099b1.

📒 Files selected for processing (2)
  • jest.setup.ts
  • src/app/api/doubts/route.ts

Walkthrough

The GET /api/doubts handler replaces a per-row correlated subquery for computing replyCount with a two-phase approach: DB-level ordering for most-replied sort, followed by batch-fetching reply counts for the returned page of doubt IDs. Additionally, a Jest mock module path was updated for the ratelimit module.

Changes

Doubts Route Performance Refactor

Layer / File(s) Summary
Batch reply count fetch replacing correlated subquery
src/app/api/doubts/route.ts
Removes the correlated replyCountSql subquery and its SELECT field, updates most-replied ordering to use a DB-level count expression, and batch-fetches reply counts for only the returned page of doubts, attaching them in-memory.
Ratelimit mock path update
jest.setup.ts
Redirects the Jest mock target from `@/lib/ratelimit` to `@/lib/ratelimit/ratelimit` while preserving the mock factory structure.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant DoubtsRoute
  participant Database

  Client->>DoubtsRoute: GET /api/doubts (sort=most-replied)
  DoubtsRoute->>Database: SELECT doubts ORDER BY reply count expression
  Database-->>DoubtsRoute: paged doubt rows
  DoubtsRoute->>Database: SELECT count(*) GROUP BY doubtId (returned IDs)
  Database-->>DoubtsRoute: reply counts map
  DoubtsRoute-->>Client: doubts JSON with replyCount attached
Loading

Possibly related issues

Possibly related PRs

  • knoxiboy/DoubtDesk#389: Also modifies the GET handler in src/app/api/doubts/route.ts for pagination/filtering and replyCount SQL computation.
  • knoxiboy/DoubtDesk#573: Also touches the Jest ratelimit mock in jest.setup.ts.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 performance change in the doubts GET handler.
Linked Issues check ✅ Passed The doubts GET query now batches reply counts for returned IDs and removes the correlated subquery from the SELECT projection, matching #809.
Out of Scope Changes check ✅ Passed The Jest mock path fix supports running tests for the main change and does not introduce unrelated scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@github-actions github-actions Bot removed the need-star label Jul 8, 2026
@codeant-ai

codeant-ai Bot commented Jul 8, 2026

Copy link
Copy Markdown

CodeAnt AI finished reviewing your PR.

@knoxiboy
knoxiboy merged commit 1745151 into knoxiboy:main Jul 8, 2026
14 of 18 checks passed
@github-actions github-actions Bot added gssoc:approved Approved for GSSoC quality:clean Clean code quality and removed size/s quality : needs-work labels Jul 8, 2026
@knoxiboy knoxiboy added the level:beginner Beginner level task label Jul 8, 2026
@github-actions github-actions Bot removed the level:advanced Advanced level task label Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Approved for GSSoC gssoc'26 GSSoC program issue level:beginner Beginner level task quality:clean Clean code quality type:bug Bug fix type:performance Performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: Doubts GET Has Correlated Subquery in SELECT

2 participants