You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/09/25 18:21:36 UTC
[GitHub] [airflow] kaxil commented on a change in pull request #11147: Massively speed up the query returned by TI.filter_for_tis
kaxil commented on a change in pull request #11147:
URL: https://github.com/apache/airflow/pull/11147#discussion_r495158034
##########
File path: airflow/models/taskinstance.py
##########
@@ -1811,20 +1811,34 @@ def filter_for_tis(
"""Returns SQLAlchemy filter to query selected task instances"""
if not tis:
return None
- if all(isinstance(t, TaskInstanceKey) for t in tis):
- filter_for_tis = ([and_(TaskInstance.dag_id == tik.dag_id,
- TaskInstance.task_id == tik.task_id,
- TaskInstance.execution_date == tik.execution_date)
- for tik in tis])
- return or_(*filter_for_tis)
- if all(isinstance(t, TaskInstance) for t in tis):
- filter_for_tis = ([and_(TaskInstance.dag_id == ti.dag_id,
- TaskInstance.task_id == ti.task_id,
- TaskInstance.execution_date == ti.execution_date)
- for ti in tis])
- return or_(*filter_for_tis)
-
- raise TypeError("All elements must have the same type: `TaskInstance` or `TaskInstanceKey`.")
+
+ # DictKeys type, (what we often pass here from the scheduler) is not directly indexable :(
+ first = list(tis)[0]
+
+ dag_id = first.dag_id
+ execution_date = first.execution_date
+ first_task_id = first.task_id
+ # Common path optimisations: when all TIs are for the same dag_id and execution_date, or same dag_id
+ # and task_id -- this can be over 150x for huge numbers of TIs (20k+)
+ if all(t.dag_id == dag_id and t.execution_date == execution_date for t in tis):
+ return and_(
+ TaskInstance.dag_id == dag_id,
+ TaskInstance.execution_date == execution_date,
+ TaskInstance.task_id.in_(t.task_id for t in tis),
+ )
+ if all(t.dag_id == dag_id and t.task_id == first_task_id for t in tis):
Review comment:
A case where there are tasks with same task_id inside the same dag should fail in Master / 2.0
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org