aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Axtens <dja@axtens.net>2018-03-12 22:07:15 +1100
committerDaniel Axtens <dja@axtens.net>2018-04-05 19:21:05 +1000
commit5d9b00409fcafc7b8cd09400195c9c9f1b3e4fcc (patch)
treed9624103c389cf507ffc8ea3800881223bd6309b
parent5bc66c8e3c9d6de9d57705ee5d2e4797e84757cb (diff)
downloadpatchwork-5d9b00409fcafc7b8cd09400195c9c9f1b3e4fcc.tar
patchwork-5d9b00409fcafc7b8cd09400195c9c9f1b3e4fcc.tar.gz
api: EventList: change select_related() to prefetch_related()
select_related() creates a single giant query that JOINs the required tables together in the DB. prefetch_related() does a similar thing, but at the Django layer - for all referenced models, it makes a separate query to the DB to fetch them. This massively, massively simplifies the job the DB has to do: instead of creating a massive, sparse results table with many columns, we do 1 query for the events, and then query for only patches/cover letters/series/projects etc referenced in those 30 events. Tested with cURL + JSON renderer + Postgres w/ ~100k patches, request time went from 1.5s to 0.25s, a 6x speedup. Tested with cURL + JSON renderer + MySQL w/ ~33k patches, request time went from ~2.2s to ~0.20s, an ~11x speedup. Signed-off-by: Daniel Axtens <dja@axtens.net>
-rw-r--r--patchwork/api/event.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/patchwork/api/event.py b/patchwork/api/event.py
index 0d97af2..7e04b71 100644
--- a/patchwork/api/event.py
+++ b/patchwork/api/event.py
@@ -96,7 +96,7 @@ class EventList(ListAPIView):
def get_queryset(self):
return Event.objects.all()\
- .select_related('project', 'patch', 'series', 'cover',
- 'previous_state', 'current_state',
- 'previous_delegate', 'current_delegate',
- 'created_check')
+ .prefetch_related('project', 'patch', 'series', 'cover',
+ 'previous_state', 'current_state',
+ 'previous_delegate', 'current_delegate',
+ 'created_check')