Skip to content

Commit ecacdda

Browse files
committed
prefetch to reduce queries in process_workflows
1 parent 1cfa515 commit ecacdda

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/sentry/workflow_engine/processors/workflow.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import sentry_sdk
66
from django.db import router, transaction
7-
from django.db.models import Q
7+
from django.db.models import Prefetch, Q
88

99
from sentry import buffer, features
1010
from sentry.db.models.manager.base_query_set import BaseQuerySet
@@ -17,6 +17,7 @@
1717
DataConditionGroup,
1818
Detector,
1919
Workflow,
20+
WorkflowDataConditionGroup,
2021
)
2122
from sentry.workflow_engine.processors.action import filter_recently_fired_workflow_actions
2223
from sentry.workflow_engine.processors.data_condition_group import process_data_condition_group
@@ -94,7 +95,7 @@ def enqueue_workflow(
9495

9596

9697
def evaluate_workflow_triggers(
97-
workflows: set[Workflow], event_data: WorkflowEventData
98+
workflows: BaseQuerySet[Workflow], event_data: WorkflowEventData
9899
) -> set[Workflow]:
99100
triggered_workflows: set[Workflow] = set()
100101

@@ -121,9 +122,10 @@ def evaluate_workflows_action_filters(
121122
) -> BaseQuerySet[Action]:
122123
filtered_action_groups: set[DataConditionGroup] = set()
123124

125+
environment_query = WorkflowDataConditionGroup.objects.select_related("workflow__environment")
124126
action_conditions = (
125127
DataConditionGroup.objects.filter(workflowdataconditiongroup__workflow__in=workflows)
126-
.prefetch_related("workflowdataconditiongroup_set")
128+
.prefetch_related(Prefetch("workflowdataconditiongroup_set", queryset=environment_query))
127129
.distinct()
128130
)
129131

@@ -219,12 +221,15 @@ def process_workflows(event_data: WorkflowEventData) -> set[Workflow]:
219221
organization = detector.project.organization
220222

221223
# Get the workflows, evaluate the when_condition_group, finally evaluate the actions for workflows that are triggered
222-
workflows = set(
224+
workflows = (
223225
Workflow.objects.filter(
224226
(Q(environment_id=None) | Q(environment_id=environment.id)),
225227
detectorworkflow__detector_id=detector.id,
226228
enabled=True,
227-
).distinct()
229+
)
230+
.select_related("when_condition_group")
231+
.prefetch_related("when_condition_group__conditions")
232+
.distinct()
228233
)
229234

230235
if workflows:

0 commit comments

Comments
 (0)