From 68e94f14186d74ff608444c1936430df016f6a96 Mon Sep 17 00:00:00 2001 From: Michael Bunsen Date: Tue, 3 Dec 2024 21:51:57 -0800 Subject: [PATCH] fix: reduce the number of queries in each page of occurrences --- ami/main/api/serializers.py | 6 +++--- ami/main/api/views.py | 2 ++ ami/main/models.py | 7 +++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ami/main/api/serializers.py b/ami/main/api/serializers.py index c36ccca3a..eb7164489 100644 --- a/ami/main/api/serializers.py +++ b/ami/main/api/serializers.py @@ -1061,7 +1061,9 @@ class Meta: ] def get_determination_details(self, obj: Occurrence): - # @TODO add an equivalent method to the Occurrence model + # @TODO convert this to query methods to avoid N+1 queries. + # Currently at 100+ queries per page of 10 occurrences. + # Add a reusable method to the OccurrenceQuerySet class and call it from the ViewSet. context = self.context @@ -1091,7 +1093,6 @@ def get_determination_details(self, obj: Occurrence): class OccurrenceSerializer(OccurrenceListSerializer): determination = CaptureTaxonSerializer(read_only=True) detections = DetectionNestedSerializer(many=True, read_only=True) - identifications = OccurrenceIdentificationSerializer(many=True, read_only=True) predictions = OccurrenceClassificationSerializer(many=True, read_only=True) deployment = DeploymentNestedSerializer(read_only=True) event = EventNestedSerializer(read_only=True) @@ -1102,7 +1103,6 @@ class Meta: fields = OccurrenceListSerializer.Meta.fields + [ "determination_id", "detections", - "identifications", "predictions", ] read_only_fields = [ diff --git a/ami/main/api/views.py b/ami/main/api/views.py index bc6ed96d1..5b1f2cf10 100644 --- a/ami/main/api/views.py +++ b/ami/main/api/views.py @@ -925,6 +925,8 @@ def get_queryset(self) -> QuerySet: "event", ) qs = qs.with_detections_count().with_timestamps() # type: ignore + qs = qs.with_identifications() # type: ignore + if self.action == "list": qs = ( qs.all() diff --git a/ami/main/models.py b/ami/main/models.py index 84a7e3bf1..fce015aab 100644 --- a/ami/main/models.py +++ b/ami/main/models.py @@ -1945,6 +1945,13 @@ def with_timestamps(self) -> models.QuerySet: ), ) + def with_identifications(self) -> models.QuerySet: + return self.prefetch_related( + "identifications", + "identifications__taxon", + "identifications__user", + ) + class OccurrenceManager(models.Manager): def get_queryset(self) -> OccurrenceQuerySet: