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

bugfix: 减少查询等待处理条件时的额外查询/提升查询性能 #7243

Open
wants to merge 1 commit into
base: feature/issue_6653
Choose a base branch
from
Open
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
13 changes: 3 additions & 10 deletions gcloud/core/apis/drf/viewsets/taskflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,9 @@ def _filter_pending_process(self):
:return:
"""
# 找到所有正在执行中的流程
# selected_related 只能在 objects 最前面加,此处先查处 ID 列表
taskflow_instance_ids: typing.List[int] = list(
self.queryset.exclude(
pipeline_instance_id__in=self._fetch_pipeline_instance_ids(statuses=[states.FAILED])
).values_list("id", flat=True)
)
# selected_related 提前查取刚需的关联数据,避免 n+1 查询
taskflow_instances: typing.List[TaskFlowInstance] = TaskFlowInstance.objects.select_related(
"pipeline_instance"
).filter(id__in=taskflow_instance_ids)
taskflow_instances = self.queryset.exclude(
pipeline_instance_id__in=self._fetch_pipeline_instance_ids(statuses=[states.FAILED])
).select_related("pipeline_instance")
# 并行找到全部的等待执行任务
pending_process_taskflow_ids: typing.List[int] = self._fetch_pending_process_taskflow_ids(taskflow_instances)
return self.queryset.filter(id__in=pending_process_taskflow_ids)
Expand Down
Loading