]> git.ozlabs.org Git - patchwork/commitdiff
views/generic_list: reduce number of queries in list rendering
authorJeremy Kerr <jk@ozlabs.org>
Wed, 7 May 2014 12:24:17 +0000 (20:24 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 8 May 2014 03:05:39 +0000 (11:05 +0800)
Currently, we do two database queries per patch on a list view; one to
retrieve the submitter, and one for the state.

This patch adds a .select_related to fetch for the submitter and state,
and a .defer() to prevent loading large amounts of text data from the
patch content and headers.

This gives a significant reduction in the work per request. For a
paginated list view (ie 100 patches per page):

                          before        after
  User                   1344 ms       228 ms
  System                  170 ms        25 ms
  Total                  1514 ms       253 ms
  Elapsed                1605 ms       274 ms

  Context switch (vol)   4206           40
  Context switch (invol)  326           75

  SQL queries             212           15
  Longest query            13 ms         5 ms
  Total query time        121 ms        20 ms

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/views/__init__.py

index 4c636182ed30fc82e3e927286776816934b85289..9d1d144fcb7b30e555a870fa80230d9d507b39d6 100644 (file)
@@ -108,6 +108,14 @@ def generic_list(request, project, view,
     if not editable_order:
         patches = order.apply(patches)
 
+    # we don't need the content or headers for a list; they're text fields
+    # that can potentially contain a lot of data
+    patches = patches.defer('content', 'headers')
+
+    # but we will need to follow the state and submitter relations for
+    # rendering the list template
+    patches = patches.select_related('state', 'submitter')
+
     paginator = Paginator(request, patches)
 
     context.update({