Skip to content

Commit 1f2c1b0

Browse files
committed
api: Optimize patch list pagination and ordering
Page over patch IDs before loading joined rows, and add indexes for project-scoped ID and date ordering. Signed-off-by: Stephen Finucane <stephen@that.guru> Assisted-by: OpenAI gpt-6.1-sol (via pi)
1 parent e137f1e commit 1f2c1b0

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎patchwork/api/patch.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from rest_framework import status
2121

2222
from patchwork.api.base import BaseHyperlinkedModelSerializer
23+
from patchwork.api.base import LinkHeaderPagination
2324
from patchwork.api.base import PatchworkPermission
2425
from patchwork.api.embedded import PatchSerializer
2526
from patchwork.api.embedded import PersonSerializer
@@ -337,11 +338,27 @@ class Meta:
337338
extra_kwargs = PatchListSerializer.Meta.extra_kwargs
338339

339340

341+
class PatchListPagination(LinkHeaderPagination):
342+
def paginate_queryset(self, queryset, request, view=None):
343+
# Page over IDs before fetching joined rows. Otherwise a deep offset
344+
# can make MySQL fetch and sort every full patch row in the project.
345+
ids = super().paginate_queryset(
346+
queryset.values_list('pk', flat=True), request, view=view
347+
)
348+
if ids is None:
349+
return None
350+
patches = {
351+
patch.pk: patch for patch in queryset.filter(pk__in=ids).order_by()
352+
}
353+
return [patches[pk] for pk in ids if pk in patches]
354+
355+
340356
class PatchList(ListAPIView):
341357
"""List patches."""
342358

343359
permission_classes = (PatchworkPermission,)
344360
serializer_class = PatchListSerializer
361+
pagination_class = PatchListPagination
345362
filter_class = filterset_class = PatchFilterSet
346363
search_fields = ('name',)
347364
ordering_fields = (
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Generated by Django 6.1.2 on 2026-10-17 03:08
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("patchwork", "0050_alter_userprofile_items_per_page"),
10+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
11+
]
12+
13+
operations = [
14+
migrations.AddIndex(
15+
model_name="patch",
16+
index=models.Index(
17+
fields=["project", "id"], name="patch_project_id_idx"
18+
),
19+
),
20+
migrations.AddIndex(
21+
model_name="patch",
22+
index=models.Index(
23+
fields=["project", "date", "id"], name="patch_project_date_idx"
24+
),
25+
),
26+
]

‎patchwork/models.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,13 @@ class Meta:
755755
],
756756
name='patch_covering_idx_default',
757757
),
758+
# REST lists can order by ID or date without filtering archived.
759+
models.Index(
760+
fields=['project', 'id'], name='patch_project_id_idx'
761+
),
762+
models.Index(
763+
fields=['project', 'date', 'id'], name='patch_project_date_idx'
764+
),
758765
]
759766

760767

‎patchwork/tests/unit/api/test_patch.py‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ def test_list_authenticated(self):
114114
patch_rsp = resp.data[0]
115115
self.assertSerialized(patch, patch_rsp)
116116

117+
def test_list_pagination_order(self):
118+
project = create_project()
119+
for name in ('d', 'a', 'e', 'b', 'c'):
120+
create_patch(project=project, name=name)
121+
create_patch() # Must not leak through the project filter.
122+
for order in ('id', '-id', 'name', '-name'):
123+
expected = list(
124+
Patch.objects.filter(project=project)
125+
.order_by(order)
126+
.values_list('id', flat=True)
127+
)[2:4]
128+
response = self.client.get(
129+
self.api_url(),
130+
{
131+
'project': project.pk,
132+
'order': order,
133+
'per_page': 2,
134+
'page': 2,
135+
},
136+
)
137+
self.assertEqual(status.HTTP_200_OK, response.status_code)
138+
self.assertEqual(expected, [p['id'] for p in response.data])
139+
self.assertIn('rel="next"', response['Link'])
140+
self.assertIn('rel="prev"', response['Link'])
141+
117142
def test_list_filter_state(self):
118143
"""Filter patches by state."""
119144
self._create_patch()
@@ -238,7 +263,8 @@ def test_list_bug_335(self):
238263
series = create_series()
239264
create_patches(5, series=series)
240265

241-
with self.assertNumQueries(5):
266+
# Count, page IDs, patch rows and the constant-size prefetches.
267+
with self.assertNumQueries(6):
242268
self.client.get(self.api_url())
243269

244270
@utils.store_samples('patch-detail')

0 commit comments

Comments
 (0)