Skip to content

Commit

Permalink
improve query performance for samples
Browse files Browse the repository at this point in the history
  • Loading branch information
atarkowska committed Dec 6, 2017
1 parent a89de97 commit cf1bf68
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
17 changes: 10 additions & 7 deletions emgapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions emgapi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions emgapi/views_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cf1bf68

Please sign in to comment.