Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConsensusDebateResult causes N+1 queries in admin results page #1704

Open
czlee opened this issue Dec 27, 2020 · 0 comments
Open

ConsensusDebateResult causes N+1 queries in admin results page #1704

czlee opened this issue Dec 27, 2020 · 0 comments
Labels
performance Issues related to the performance
Milestone

Comments

@czlee
Copy link
Member

czlee commented Dec 27, 2020

On the main admin results page, this query occurs as many times as there are debates:

SELECT (1) AS "a"
  FROM "tournaments_round"
 WHERE ("tournaments_round"."tournament_id" = 1 AND "tournaments_round"."seq" > 1)
 LIMIT 1

image

Last few lines of stack trace:

/home/czlee/tabbycat/tabbycat/tournaments/models.py in debate_set_with_prefetches(519)
  populate_confirmed_ballots(debates, motions=True, results=True)
/home/czlee/tabbycat/tabbycat/results/prefetch.py in populate_confirmed_ballots(66)
  populate_results(confirmed_ballots)
/home/czlee/tabbycat/tabbycat/results/prefetch.py in populate_results(100)
  result.init_blank_buffer()
/home/czlee/tabbycat/tabbycat/results/result.py in init_blank_buffer(624)
  super().init_blank_buffer()
/home/czlee/tabbycat/tabbycat/results/result.py in init_blank_buffer(765)
  if len(self.sides) == 4 and self.debate.round.is_last:
/home/czlee/tabbycat/tabbycat/tournaments/models.py in is_last(579)
  return not self._rounds_in_same_sequence().filter(seq__gt=self.seq).order_by('seq').exists()

I think the check for whether there should be one or two winners in BP debates is causing it:

def init_blank_buffer(self):
super().init_blank_buffer()
self.scoresheet = self.scoresheet_class(positions=getattr(self, 'positions', None))
if len(self.sides) == 4 and self.debate.round.is_last:
self.scoresheet.number_winners = 1

The quick hackish way to result this would be to make Round.is_last a cached_property, but I didn't want to just do that in case there was some structurally better way to go about this, following the refactoring in #1180. We presumably want the information about the round to be passed cleanly to all the result objects or something, from populate_results() or maybe even from debate_set_with_prefetches(). (But I'm fine with just modifying Round.is_last if that's what should happen.)

(@tienne-B, thoughts?)

@czlee czlee added the performance Issues related to the performance label Dec 27, 2020
@czlee czlee added this to the Ocicat milestone Dec 27, 2020
tienne-B added a commit that referenced this issue Jan 6, 2021
To avoid calling round.is_last as often, the conditions that lead to
that point were expanded and analyzed (added parentheses too) to make
sure the right result class is used.

Then, for ConsensusDebateResult, the condition for having just 1 team
advance in BP now directly tests if the scoresheet is correct. So, the
.is_last method is only called for BP outrounds.

Ref #1704
tienne-B added a commit that referenced this issue Jan 6, 2021
To avoid calling round.is_last as often, the conditions that lead to
that point were expanded and analyzed (added parentheses too) to make
sure the right result class is used.

Then, for ConsensusDebateResult, the condition for having just 1 team
advance in BP now directly tests if the scoresheet is correct. So, the
.is_last method is only called for BP outrounds.

Ref #1704
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Issues related to the performance
Projects
None yet
Development

No branches or pull requests

1 participant