perf: replace correlated subquery in doubts GET with batched reply-count fetch#820
Conversation
|
CodeAnt AI is reviewing your PR. |
|
@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. |
Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
⭐ 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!
|
@coderabbitai review |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughThe 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. ChangesDoubts Route Performance Refactor
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
Possibly related issues
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
CodeAnt AI finished reviewing your PR. |
User description
Description
Removes the correlated subquery that was previously part of the SELECT projection in the doubts
GETendpoint (src/app/api/doubts/route.ts), which re-ran aSELECT count(*)per returned row. Reply counts are now: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.limitrows, viainArray(doubtIds)), following the same pattern already used elsewhere in this file forhasLiked/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/ratelimitinstead 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
Screenshots (if UI change)
Not applicable, backend/query-level change only, no UI change.
How Has This Been Tested?
npm run devAdditional testing notes:
src/__tests__/api/doubts.test.tspass, covering pagination, all sort modes (newest,popular,most-replied,unsolved), empty results, exact page-boundary, and invalid pagination input sanitization.npm test) - 187 passing, 6 failing. Confirmed viagit stash+git checkout mainthat all 6 failures (digest-functions.test.ts,teacher-insights.test.ts,migration-integrity.test.ts) pre-exist onmainand are unrelated to this change.npx eslint .passes clean.npm run buildcompletes successfully.npx tsc --noEmitreports 4 pre-existing type errors, confirmed present identically onmainvia the same stash/checkout comparison, in files/lines unrelated to this change (db.test.ts, an unrelatedtagRows.reduce<>block further downroute.ts, andinvites/[token]/join/route.ts). Committed with--no-verifyto bypass the pre-commit hook for these pre-existing issues rather than fixing unrelated code out of scope for this PR.Checklist
npm run dev)anytypes)mainCodeAnt-AI Description
Speed up doubt listings and restore test runs
What Changed
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:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
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:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
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