From 60cf2dcbd7a7068d0a7300df5acf9e8e7fad3593 Mon Sep 17 00:00:00 2001 From: Jens Gutermuth Date: Mon, 23 Mar 2015 01:58:12 +0100 Subject: [PATCH] Fix three N+1 queries in pin loading Fetch the submitter with a join and get all the images and tags in one query each. This reduces the total query count for loading 50 pins from 304 to 158. See issue #85. --- pinry/core/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pinry/core/api.py b/pinry/core/api.py index 56a4b21..334c019 100644 --- a/pinry/core/api.py +++ b/pinry/core/api.py @@ -135,7 +135,8 @@ class PinResource(ModelResource): filtering = { 'submitter': ALL_WITH_RELATIONS } - queryset = Pin.objects.all() + queryset = Pin.objects.all().select_related('submitter'). \ + prefetch_related('image', 'tags') resource_name = 'pin' include_resource_uri = False always_return_data = True