Skip to content

Conversation

@Rashmil-1999
Copy link
Contributor

@Rashmil-1999 Rashmil-1999 commented Dec 1, 2025

Looking at the diff, I can provide a clear explanation of this performance optimization:

Pull Request Summary

Performance Optimization: Document Count API Endpoint

Performance Gain: ~90% faster (3724ms → 386ms)

This PR introduces a dedicated lightweight endpoint for fetching document counts, resulting in a 89.6% reduction in response time.

Changes Made:

  1. New Endpoint: Created /api/materialsTable/fetchDocumentCount.ts

    • Implements an optimized query that checks for document existence using .limit(1) instead of counting all documents
    • Returns a binary result: 1 if documents exist, 0 if none exist
    • Includes proper authorization via withCourseAccessFromRequest
  2. Frontend Update: Modified src/pages/[course_name]/chat.tsx

    • Switched from calling fetchProjectMaterials (which fetches full document data with pagination)
    • Now calls the new dedicated fetchDocumentCount endpoint

Performance Impact:

  • Before: 3,724 ms (fetching full project materials with pagination)
  • After: 386 ms (simple existence check query)
  • Improvement: 3,338 ms saved per request (89.6% faster)
  • Speed multiplier: ~9.6x faster

Technical Approach:

Instead of fetching and paginating through all documents, the new endpoint uses a simple SQL LIMIT 1 query to check if any documents exist for the course. This dramatically reduces database load and query execution time while providing the same functional result for the UI.

@vercel
Copy link

vercel bot commented Dec 1, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
uiuc-chat-frontend Ready Ready Preview, Comment Jan 12, 2026 9:27pm

Copy link
Collaborator

@rohan-uiuc rohan-uiuc left a comment

Choose a reason for hiding this comment

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

Let's change this to fetch a single id from documents table where course_name is a match as that will be way faster that getting document count for projects with large number of documents. Additionally, we don't need filters in this API as they are not used anywhere.

@Rashmil-1999
Copy link
Contributor Author

@rohan-uiuc I guess it is ready for another peek

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.

Document Count fetch delay causes document count to remain 0 until it is fetched

3 participants