Skip to content

Commit

Permalink
fix: reduce the number of queries in each page of occurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
mihow committed Dec 4, 2024
1 parent bf04b6b commit 68e94f1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ami/main/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -1102,7 +1103,6 @@ class Meta:
fields = OccurrenceListSerializer.Meta.fields + [
"determination_id",
"detections",
"identifications",
"predictions",
]
read_only_fields = [
Expand Down
2 changes: 2 additions & 0 deletions ami/main/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions ami/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 68e94f1

Please sign in to comment.