rwarbelow/the_swot_bot — pull request #258 by dugancathal (2013-10-04 01:39:16+00:00)
Title: Speed up "/teachers/courses/show" page

The slowness was a result of the weighted grade calculation using almost
60 queries per student. In order to speed up the response time for this page,
we encapsulated the logic for grade calculation in its own
[GradeCalculator](app/models/grade_calculator.rb) class.

Through some clever usage of SQL JOIN and GROUP statements, we were able
to cut the time down to a tenth (1/10) of its former speed. In that
file, lines 20-25 are of interest in the realm of SQL. Above that, in
the `#grade` method, it follows the same algorithm that was being used
previously, it just pushes all the data gathering to the database.

It also:

* Removes the `Student#find_submissions` method

* Renames the `Student#calculate_percent` method to `Student#grade_in`

* Removes the `Course.calculate_student_percent`

* Updates the /teachers/courses/show.html.erb page to use the new
methods

Before patch:

* Course with 9 assignments and 17 students:

* Minimum 2.714 seconds to load - 632 queries
* Maximum 3.308 seconds to load - 632 queries

After patch:

* Course with 9 assignments and 17 students (same):

* Minimum .2440 seconds to load - 44 queries
* Maximum .3026 seconds to load - 44 queries

Finally, I added a few Factories to make life easier on this one.