Reduce SQL queries for some of the legacy API methods

Review Request #1895 — Created Nov. 7, 2010 and submitted


Review Board


This is similar to my changes in Djblets in It uses select_related ( to reduce the number of queries needed to serve resources. Select_related will use joins to include one-to-one relations in the initial query, meaning you won't have to do another query to fetch them after the main object is retrieved.

This contains three changes:
* Optimize legacy API database access for lists of reviews. Goes from 4 + 5*n round trips to 3 + 2*n round trips.
* Add select_related() to some more legacy APIs to reduce their DB round-trips.
* Reduce SQL calls for getting repositories using select_related.

Note that there is also a change to get_from_user_query which avoids a user lookup if you already have a username by using a join. The downside is that if users stop being uniquely identified by username, this would break, but that change seems unlikely.
Ran ReviewBoard with SQL logging and confirmed that this produces the same results with fewer round trips.
  1. Thanks! Committed with some wrapping fixes (< 80 columns) to master (f171553).