ccnmtl/mvsim — Issue #1: Admin > CourseSection add/edit forms are unusably slow to load
Author: ejucovy
Created: 2011-10-12 13:55:44+00:00
State: open

On the production site at least, loading the Django Admin add/edit forms for course sections can take upwards of 30s, making them frustrating in the best case, and actually unusable in the worst case.

My suspicion is that this is because:

1) The "starting state" multiselect widget loads all available states, which includes all turns played in all games. This is over 1k states.

2) The `__unicode__` method of the `main.State` model, which is called for each of the states, follows the state's foreign-key relationship to `main.Game` *and also* iterates over all the states related to that `main.Game` in order to determine this state's turn number. This only happens if the `State` instance doesn't have a `name` attribute.

3) There's no `select_related()` directive anywhere, so an additional separate database query is issued for each of the 1k states.

Although obviously the `__unicode__` method is implemented badly, a quicker and less invasive fix would completely work around this problem:

The "starting state" multiselect widget should use a custom queryset: `State.objects.filter(game__isnull=True)`. The only states eligible to be starting states should be those that aren't already associated with a game (i.e. a turn in a real game) -- in order to make a starting state out of a game turn, an instructor would clone that game turn.

This would fix the problem, because states with a null Game FK are required to have a `name` attribute anyway, so none of the additional queries would occur.