From cf1bf6847236c4dcfc5ef8dd8187b40be36b0498 Mon Sep 17 00:00:00 2001 From: Ola Tarkowska Date: Wed, 6 Dec 2017 10:37:28 +0000 Subject: [PATCH] improve query performance for samples --- emgapi/models.py | 17 ++++++++++------- emgapi/serializers.py | 6 ++++-- emgapi/views_relations.py | 2 -- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/emgapi/models.py b/emgapi/models.py index a397de06a..59fbdede6 100644 --- a/emgapi/models.py +++ b/emgapi/models.py @@ -419,17 +419,22 @@ def get_queryset(self): def available(self, request, prefetch=False): queryset = self.get_queryset().available(request) if prefetch: - _qs = SampleAnn.objects.all() queryset = queryset.extra( { 'longitude': "CAST(longitude as DECIMAL(10,5))", 'latitude': "CAST(latitude as DECIMAL(10,5))" } - ) - queryset = queryset.prefetch_related( + ).prefetch_related( Prefetch('biome', queryset=Biome.objects.all()), Prefetch('studies', queryset=Study.objects.available(request)), - Prefetch('metadata', queryset=_qs) + Prefetch('metadata', queryset=SampleAnn.objects.all()) + ).defer( + 'primary_accession', + 'is_public', + 'metadata_received', + 'sequencedata_received', + 'sequencedata_archived', + 'submission_account_id', ) return queryset @@ -829,9 +834,7 @@ class SampleAnnManager(models.Manager): def get_queryset(self): return SampleAnnQuerySet(self.model, using=self._db) \ - .prefetch_related( - Prefetch('var', queryset=VariableNames.objects.all()) - ) + .select_related('var') class SampleAnn(models.Model): diff --git a/emgapi/serializers.py b/emgapi/serializers.py index 303beb466..1763dc4a1 100644 --- a/emgapi/serializers.py +++ b/emgapi/serializers.py @@ -684,7 +684,7 @@ def get_biosample(self, obj): ) def get_studies(self, obj): - return obj.studies.all() + return obj.studies.available(self.context['request']) runs = relations.SerializerMethodResourceRelatedField( source='get_runs', @@ -757,7 +757,9 @@ def get_bioproject(self, obj): ) def get_biomes(self, obj): - biomes = obj.samples.values('biome_id').distinct() + biomes = obj.samples \ + .available(self.context['request']) \ + .values('biome_id').distinct() return emg_models.Biome.objects \ .filter(pk__in=biomes) diff --git a/emgapi/views_relations.py b/emgapi/views_relations.py index 523bc696b..2ed7e5317 100644 --- a/emgapi/views_relations.py +++ b/emgapi/views_relations.py @@ -111,8 +111,6 @@ def get_queryset(self): .available(self.request, prefetch=True) \ .filter(studies__in=[study]) _qs = emg_models.Study.objects.available(self.request) - # queryset = queryset.prefetch_related( - # Prefetch('studies', queryset=_qs)) if 'runs' in self.request.GET.get('include', '').split(','): _qs = emg_models.Run.objects \ .available(self.request)