Description:
The original code implemented this by iterating over all universes using Python's built-in sorted() function. Inside the key lambda for sorting, it called .get() on the ExerciseUniverses through-model for each item to check if it was marked as default (is_default).
This logic resulted in a classic N+1 query problem. For every list of size N, the system would execute 1 initial query to get all universes, plus N individual database queries to check the is_default flag. f the database grows in the future, this will generate an unnecessary, unscalable load directly proportional to the number of universes.
Description:
The original code implemented this by iterating over all universes using Python's built-in sorted() function. Inside the key lambda for sorting, it called .get() on the ExerciseUniverses through-model for each item to check if it was marked as default (is_default).
This logic resulted in a classic N+1 query problem. For every list of size N, the system would execute 1 initial query to get all universes, plus N individual database queries to check the is_default flag. f the database grows in the future, this will generate an unnecessary, unscalable load directly proportional to the number of universes.