Skip to content

Add database indexes to improve query performance#113

Open
LoriXu430 wants to merge 1 commit intomainfrom
devin/1760938672-add-database-indexes
Open

Add database indexes to improve query performance#113
LoriXu430 wants to merge 1 commit intomainfrom
devin/1760938672-add-database-indexes

Conversation

@LoriXu430
Copy link

Add database indexes to improve query performance

Summary

This PR adds database indexes to 5 frequently queried model fields to improve search and lookup performance:

  • Question model: Added indexes to filename and Hash fields
  • Course model: Added indexes to name and university fields
  • UploadedFile model: Added index to filename field

These fields are used extensively in filter() and get() operations throughout the codebase, particularly in search functionality (search.py), PDF reader lookups (views.py), and upload operations (upload.py). As the database grows, these indexes will significantly reduce query times by avoiding full table scans.

Review & Testing Checklist for Human

  • Generate the migration file: Run python manage.py makemigrations to create the migration that adds these indexes. Review the generated migration to ensure it looks correct.
  • Test migration locally: Apply the migration on a development database with python manage.py migrate and verify no errors occur.
  • Verify TextField indexes are appropriate: Confirm that adding db_index=True to TextField fields works as expected with PostgreSQL. Consider whether any of these fields would benefit from full-text search indexes instead.
  • Plan production deployment: Adding indexes to existing tables can take time and may cause locks. Plan to deploy during low-traffic period if tables contain significant data.
  • Test query performance (optional): Use Django Debug Toolbar or database EXPLAIN ANALYZE to verify that queries using these fields now use the indexes and show improved performance.

Notes

  • The changes are purely additive (adding indexes) and don't modify any existing data or logic
  • No migration file is included in this PR - it must be generated by running makemigrations
  • The indexed fields were identified through code analysis of search operations, file lookups, and upload handlers
  • Consider monitoring query performance in production after deployment to validate the improvement

Link to Devin run: https://app.devin.ai/sessions/5d7be24cadee44318538178baa782d1b
Requested by: 治乾徐 (xuzhiqian.430@bytedance.com) / @LoriXu430

- Add db_index=True to Question.filename and Question.Hash
- Add db_index=True to Course.name and Course.university
- Add db_index=True to UploadedFile.filename

These indexes will significantly improve query performance for search
operations and file lookups, especially as the database grows. The
indexed fields are frequently used in filter() and get() operations
throughout the codebase.

Co-Authored-By: 治乾徐 <18948336448@163.com>
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.

1 participant