~james-w/python-oops-tools/prod-deploy

« back to all changes in this revision

Viewing changes to src/oopstools/oops/views.py

  • Committer: Tarmac
  • Author(s): James Westby
  • Date: 2012-12-06 02:10:46 UTC
  • mfrom: (50.1.3 recent-oopses)
  • Revision ID: launchpad@pqm.canonical.com-20121206021046-q5tjvtab4ksawk3z
Improve the query performance of the recent oopses view.

Firstly the planner was picking a difficult plan because of the join, but
we expect few prefixes per-report, so getting the prefix ids first, then
using a simple IN rather than a join leads to an easier plan.

Once the easier plan is in use the indexes still aren't that helpful, as
it chooses the index on 'date', but it still has to filter based on the
prefix, so we add an index on (prefix_id, date) so that it can select the
right rows as quickly as possible.

Lastly lazy evaluation was causing 50 queries for the 'oopsinfestation'
info, so we use select_related() to include that in the main query.

There's a test that we do 1 query for the last point, but no test for
the speed of the query.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
        dates = []
136
136
        for day in range(1, 11):
137
137
            dates.append(now - timedelta(day))
 
138
        prefix_ids = list(r.prefixes.values_list('pk', flat=True))
138
139
        oopses = Oops.objects.filter(
139
 
                prefix__report=r).order_by('-date')
 
140
                prefix__in=prefix_ids).select_related(
 
141
                    'oopsinfestation').order_by('-date')
140
142
        paginator = Paginator(oopses, 50)
141
143
        recent_oopses = get_page_from_query_args(paginator, request.GET)
142
144
        data = {