Add more non-unique indices for read performance#845
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds database indices to improve read performance across multiple models in the penn-clubs application. The changes focus on fields that are frequently used in filtering, ordering, and JOIN operations based on analysis of existing query patterns.
- Adds single-field indices on boolean and datetime fields used for filtering
- Creates composite indices for commonly combined filter conditions
- Targets models like Club, Event, Membership, and Ticket that are central to the application's read-heavy workload
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| class Meta: | ||
| indexes = [ | ||
| models.Index(fields=["event", "start_time"]), |
There was a problem hiding this comment.
[nitpick] The composite index on ['event', 'start_time'] may be redundant since 'event' is already a ForeignKey which typically has an implicit index. Consider whether a single-field index on 'start_time' would be sufficient for the query patterns, or if the composite index provides additional optimization for specific queries that filter by both event and start_time together.
| models.Index(fields=["event", "start_time"]), |
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #845 +/- ##
==========================================
+ Coverage 74.57% 74.60% +0.02%
==========================================
Files 33 33
Lines 7852 7861 +9
==========================================
+ Hits 5856 5865 +9
Misses 1996 1996 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Asked Claude Code to read through our read queries and add indices as recommended. In general, the app is read-heavy (or at the very least write-light with the possible exception of applications, but even then the degraded application write performance seemed to be from system-wide constraints), so adding these queries is low risk from a performance standpoint. However, these could increase storage costs somewhat.
Here is the list of indices + usages it came up with:
Database Index Changes (vs master)
Club Model
archived— Excludes archived clubs (views.py:1224)approved_on— Existing index for approval date queries(approved, archived)— Combined filtering for active, non-archived clubs (views.py:1224)approved(single-column; covered by composite and low selectivity) (views.py:1234)Event Model
type— Fair event filtering (views.py:739, views.py:913)ics_uuid— ICS event lookups (models.py:481)EventShowing Model
end_time— Event end time filtering with ranges (views.py:3138)(start_time, end_time)— Time range queries (views.py:3138, views.py:5121)(event, start_time)— Event-scoped time queries (filtering showings via parent event) (views.py:913, views.py:915, views.py:3204)start_time(single-column; covered by composites)ClubFair Model
end_time— Current/active fair filtering (views.py:699)(end_time, start_time)— Composite to support upcoming/current lists and ordering (views.py:1096)Membership Model
role— Officer/member permission checks (views.py:2471)(person, club, role)— Low benefit given existing unique(club, person)and observed query patternsClubVisit Model
(club, visit_type, created_at)— Time-series visit analytics (views.py:1637)visit_type(single-column; analytics use composite)ClubApplication Model
application_start_time— Application period filtering (views.py:7343)application_end_time— Deadline checking (views.py:7343)result_release_time— Result release schedulingApplicationSubmission Model
status— Filter/aggregate by acceptance/rejection status (views.py:7714)Ticket Model
type— Ticket type filtering (views.py:3494)(showing, type)— Available tickets by type per showing (views.py:3494)(showing, owner)— Ticket ownership queries (views.py:3333)(showing, holder)— Held ticket queries(showing, type) WHERE owner IS NULL AND holder IS NULL AND buyable = TRUE— Partial index for fast “available tickets” lookups (views.py:3493)Badge Model
purpose— Fair badge filtering (views.py:924)