diff --git a/src/sentry/api/endpoints/project_environments.py b/src/sentry/api/endpoints/project_environments.py index 0671d6a3e662f4..8b3a6d758903c3 100644 --- a/src/sentry/api/endpoints/project_environments.py +++ b/src/sentry/api/endpoints/project_environments.py @@ -45,6 +45,8 @@ def get(self, request, project): queryset = EnvironmentProject.objects.filter( project=project, + # Including the organization_id is necessary for postgres to use indexes efficiently. + environment__organization_id=project.organization_id ).exclude( # HACK(mattrobenolt): We don't want to surface the # "No Environment" environment to the UI since it diff --git a/src/sentry/api/serializers/models/project.py b/src/sentry/api/serializers/models/project.py index 27d43862e88463..5020481d5cfb72 100644 --- a/src/sentry/api/serializers/models/project.py +++ b/src/sentry/api/serializers/models/project.py @@ -272,10 +272,12 @@ def get_attrs(self, item_list, user): project_envs = EnvironmentProject.objects.filter( project_id__in=[i.id for i in item_list], + # Including the organization_id is necessary for postgres to use indexes efficiently. + environment__organization_id=item_list[0].organization_id ).exclude( - is_hidden=True - ).exclude( + is_hidden=True, # HACK(lb): avoiding the no environment value + ).exclude( environment__name='' ).values('project_id', 'environment__name')