Reduce SQL queries for some of the legacy API methods

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

Information

Review Board
https://github.com/bhollis/reviewboard/tree/db_queries

Reviewers

This is similar to my changes in Djblets in http://reviews.reviewboard.org/r/1894/. It uses select_related (http://docs.djangoproject.com/en/dev/ref/models/querysets/#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.
chipx86
  1. Thanks! Committed with some wrapping fixes (< 80 columns) to master (f171553).
  2.