Skip to content

Commit

Permalink
perf: reduce ORM lookups (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
goodtune authored May 8, 2024
1 parent bb6cbfa commit 1183e3b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
6 changes: 4 additions & 2 deletions tournamentcontrol/competition/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1875,8 +1875,10 @@ def match_schedule(
if time is not None:
where &= Q(time=time)

queryset = manager.filter(where).order_by(
"stage__division__order", "round", "datetime", "play_at"
queryset = (
manager.select_related("stage__division__season")
.filter(where)
.order_by("stage__division__order", "round", "datetime", "play_at")
)

return self.generic_edit_multiple(
Expand Down
4 changes: 2 additions & 2 deletions tournamentcontrol/competition/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ def __init__(self, *args, **kwargs):

# If the season has timeslot rules, substitute the default SelectTime
# field for a drop-down list of timeslots.
if self.instance.stage.division.season.timeslots.count():
if self.instance.stage.division.season.timeslots.exists():
timeslots = self.instance.stage.division.season.get_timeslots(
self.instance.date
)
Expand Down Expand Up @@ -1279,7 +1279,7 @@ def label_from_instance(obj):
for venue in self.instance.stage.division.season.venues.all():
venues.setdefault(venue, [])

for ground in Ground.objects.filter(venue__in=venues):
for ground in Ground.objects.select_related("venue").filter(venue__in=venues):
venues[ground.venue].append(ground)

places = FauxQueryset(Place)
Expand Down
2 changes: 1 addition & 1 deletion tournamentcontrol/competition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ def get_places(self):
.difference(grounds.values_list("venue", flat=True))
.union(grounds.values_list("pk", flat=True))
)
return Place.objects.filter(pk__in=pks)
return Place.objects.filter(pk__in=pks).select_related("venue", "ground__venue")

def get_timeslots(self, date=None):
# work out the timeslot rules to exclude
Expand Down
1 change: 1 addition & 0 deletions tournamentcontrol/competition/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ def generate_fixture_grid(
"home_team__club",
"away_team",
"away_team__club",
"play_at",
).filter(date=date)

times = sorted(
Expand Down

0 comments on commit 1183e3b

Please sign in to comment.