penn-clubs icon indicating copy to clipboard operation
penn-clubs copied to clipboard

Optimize assorted Django operations

Open aviupadhyayula opened this issue 11 months ago • 1 comments

Meant to generally optimize our usage of the Django ORM, with a focus on taking full advantage of major upgrades in #605.

Changelog:

  • Use subqueries to update membership & favorite counts in update_club_counts. Shows a ~70% speedup in local benchmarks (n=2000 memberships/favorites).
  • Use bulk updates when updating decision reasons for application submissions.
  • Use bulk create when creating activities fairs for registered clubs.
    • Concerned with how bulk_create plays with AutoField primary keys (like we have for the Events model). But, the documentation mentions that pSQL should be fine.[1]
    • I'm assuming the order of the returned Events objects doesn't matter. Would appreciate a second set of eyes.
  • Use qs.exists() to check existence instead of qs.first() is not None whenever possible.
  • Avoid fetching objects from DB into memory whenever possible.

[1] https://docs.djangoproject.com/en/5.0/ref/models/querysets/#bulk-create

aviupadhyayula avatar Mar 10 '24 00:03 aviupadhyayula

Codecov Report

Attention: Patch coverage is 48.85496% with 67 lines in your changes are missing coverage. Please review.

Project coverage is 69.29%. Comparing base (813cd4b) to head (c5a912d). Report is 20 commits behind head on master.

Files Patch % Lines
backend/clubs/views.py 44.82% 64 Missing :warning:
...nd/clubs/management/commands/update_club_counts.py 25.00% 3 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #627      +/-   ##
==========================================
+ Coverage   69.11%   69.29%   +0.17%     
==========================================
  Files          30       30              
  Lines        6362     6353       -9     
==========================================
+ Hits         4397     4402       +5     
+ Misses       1965     1951      -14     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Mar 10 '24 04:03 codecov[bot]