Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ [PERF] Optimize some queries #4277

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CHANGELOG
**Maintenance**

- Replace deprecated `env_file` with `dotenv`
- Optimize some backend queries for performances


2.109.0 (2024-08-08)
Expand Down
14 changes: 10 additions & 4 deletions geotrek/land/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,34 @@ class Meta(OrganismFilterSet.Meta):
"""


class TopologyFilterPhysicalType(TopologyFilter):
class PrefetchStructureMixin:
def get_queryset(self, request=None):
qs = super().get_queryset(request)
return qs.select_related('structure')


class TopologyFilterPhysicalType(PrefetchStructureMixin, TopologyFilter):
model = PhysicalType

def values_to_edges(self, values):
return PhysicalEdge.objects.filter(physical_type__in=values)


class TopologyFilterCirculationType(TopologyFilter):
class TopologyFilterCirculationType(PrefetchStructureMixin, TopologyFilter):
model = CirculationType

def values_to_edges(self, values):
return CirculationEdge.objects.filter(circulation_type__in=values)


class TopologyFilterAuthorizationType(TopologyFilter):
class TopologyFilterAuthorizationType(PrefetchStructureMixin, TopologyFilter):
model = AuthorizationType

def values_to_edges(self, values):
return CirculationEdge.objects.filter(authorization_type__in=values)


class TopologyFilterLandType(TopologyFilter):
class TopologyFilterLandType(PrefetchStructureMixin, TopologyFilter):
model = LandType

def values_to_edges(self, values):
Expand Down
4 changes: 2 additions & 2 deletions geotrek/land/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def physical_type_csv_display(self):

@classmethod
def path_physicals(cls, path):
return cls.objects.existing().select_related('physical_type').filter(aggregations__path=path).distinct('pk')
return cls.objects.existing().select_related('physical_type', 'physical_type__structure').filter(aggregations__path=path).distinct('pk')

@classmethod
def topology_physicals(cls, topology):
return cls.overlapping(topology).select_related('physical_type')
return cls.overlapping(topology).select_related('physical_type', 'physical_type__structure')


@receiver(pre_delete, sender=Topology)
Expand Down
4 changes: 4 additions & 0 deletions geotrek/maintenance/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class ProjectIntersectionFilterDistrict(PolygonProjectFilterMixin, RightFilter):
class ProjectIntersectionFilterRestrictedArea(PolygonProjectFilterMixin, RightFilter):
model = RestrictedArea

def get_queryset(self, request=None):
qs = super().get_queryset(request)
return qs.select_related('area_type')


class ProjectIntersectionFilterRestrictedAreaType(PolygonProjectFilterMixin, RightFilter):
model = RestrictedAreaType
Expand Down
4 changes: 2 additions & 2 deletions geotrek/outdoor/templatetags/outdoor_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def site_practices():
for scale in practice.rating_scales.all()
},
}
for practice in Practice.objects.all()
for practice in Practice.objects.prefetch_related('site_types', 'rating_scales').all()
}
return json.dumps(practices)

Expand All @@ -44,7 +44,7 @@ def course_sites():
for scale in site.practice.rating_scales.all()
},
} if not (site.practice is None) else {'practice': None, 'types': {}, 'scales': {}}
for site in Site.objects.all()
for site in Site.objects.select_related('practice').prefetch_related('practice__course_types', 'practice__rating_scales').all()
}
return json.dumps(sites)

Expand Down
4 changes: 2 additions & 2 deletions geotrek/tourism/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class TypeFilter(ModelMultipleChoiceFilter):


class TouristicContentFilterSet(ZoningFilterSet, StructureRelatedFilterSet):
type1 = TypeFilter(queryset=TouristicContentType1.objects.all())
type2 = TypeFilter(queryset=TouristicContentType2.objects.all())
type1 = TypeFilter(queryset=TouristicContentType1.objects.select_related('category').all())
type2 = TypeFilter(queryset=TouristicContentType2.objects.select_related('category').all())
provider = ChoiceFilter(
field_name='provider',
empty_label=_("Provider"),
Expand Down
6 changes: 3 additions & 3 deletions geotrek/tourism/templatetags/tourism_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def touristic_content_categories():
'type2_label': category.type2_label,
'type1_values': {
str(type.pk): type.label
for type in category.types.filter(in_list=1)
for type in category.types.all() if type.in_list == 1
},
'type2_values': {
str(type.pk): type.label
for type in category.types.filter(in_list=2)
for type in category.types.all() if type.in_list == 2
},
'geometry_type': category.geometry_type
}
for category in TouristicContentCategory.objects.all()
for category in TouristicContentCategory.objects.prefetch_related('types').all()
}
return json.dumps(categories)

Expand Down
2 changes: 1 addition & 1 deletion geotrek/trekking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def topology_all_pois(cls, topology, queryset=None):
Transform(F('geom'), settings.SRID)))
qs = qs.order_by('locate')

return qs
return qs.select_related("type")

@classmethod
def outdoor_all_pois(cls, obj):
Expand Down
2 changes: 1 addition & 1 deletion geotrek/trekking/templatetags/trekking_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def trek_practices():
for scale in practice.rating_scales.all()
},
}
for practice in Practice.objects.all().prefetch_related('rating_scales')
for practice in Practice.objects.prefetch_related('rating_scales').all()
}
return json.dumps(practices)

Expand Down
4 changes: 2 additions & 2 deletions geotrek/trekking/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def render_to_response(self, context):


class TrekDetail(CompletenessMixin, MapEntityDetail):
queryset = Trek.objects.existing().select_related('topo_object').prefetch_related(
queryset = Trek.objects.existing().select_related('topo_object', 'structure').prefetch_related(
Prefetch('view_points',
queryset=HDViewPoint.objects.select_related('content_type', 'license'))
)
Expand Down Expand Up @@ -292,7 +292,7 @@ class POIDetail(CompletenessMixin, MapEntityDetail):
queryset = POI.objects.existing().prefetch_related(
Prefetch('view_points',
queryset=HDViewPoint.objects.select_related('content_type', 'license'))
)
).select_related('type')

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
Expand Down
Loading