Skip to content

Commit 37380c7

Browse files
authored
ref(environments) Optimize environment queries (#14102)
Adding the organization_id to these queries allows postgres to use indexes better and makes queries much faster for organizations with many environments.
1 parent ea051b2 commit 37380c7

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/sentry/api/endpoints/project_environments.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def get(self, request, project):
4545

4646
queryset = EnvironmentProject.objects.filter(
4747
project=project,
48+
# Including the organization_id is necessary for postgres to use indexes efficiently.
49+
environment__organization_id=project.organization_id
4850
).exclude(
4951
# HACK(mattrobenolt): We don't want to surface the
5052
# "No Environment" environment to the UI since it

src/sentry/api/serializers/models/project.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,12 @@ def get_attrs(self, item_list, user):
272272

273273
project_envs = EnvironmentProject.objects.filter(
274274
project_id__in=[i.id for i in item_list],
275+
# Including the organization_id is necessary for postgres to use indexes efficiently.
276+
environment__organization_id=item_list[0].organization_id
275277
).exclude(
276-
is_hidden=True
277-
).exclude(
278+
is_hidden=True,
278279
# HACK(lb): avoiding the no environment value
280+
).exclude(
279281
environment__name=''
280282
).values('project_id', 'environment__name')
281283

0 commit comments

Comments
 (0)