Commit 8dfd85b
authored
Fix N+1 query problem in GET /api/v1/changesets/ (#744)
The DRF Serializer for the Changeset model was using obj.tags.filter()
and obj.reasons.filter() to filter the lists of reasons and tags
(only showing enabled ones to non-staff users). This had the probably
unexpected consequence of generating an additional two database
queries for each changeset in the resulting list. The reasons and tags
related fields were being prefetched, but prefetching only works for
iterating the entire relation.
The fix for this problem is straightforward: instead of using .filter(),
we can just filter the reasons and tags in Python. This allows the
prefetching to work as intended and avoids the additional queries.1 parent 7159e7a commit 8dfd85b
1 file changed
+9
-10
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
71 | 70 | | |
72 | 71 | | |
73 | 72 | | |
| |||
0 commit comments