mik-laj commented on a change in pull request #10956:
URL: https://github.com/apache/airflow/pull/10956#discussion_r499494587



##########
File path: airflow/models/dag.py
##########
@@ -1572,6 +1661,21 @@ def bulk_sync_to_db(cls, dags: Collection["DAG"], 
sync_time=None, session=None):
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
+        # Get the latest dag run for each existing dag as a single query 
(avoid n+1 query)
+        most_recent_dag_runs = dict(session.query(DagRun.dag_id, 
func.max_(DagRun.execution_date)).filter(
+            DagRun.dag_id.in_(existing_dag_ids),
+            or_(
+                DagRun.run_type == DagRunType.BACKFILL_JOB.value,
+                DagRun.run_type == DagRunType.SCHEDULED.value,
+            ),
+        ).group_by(DagRun.dag_id).all())
+
+        num_active_runs = dict(session.query(DagRun.dag_id, 
func.count('*')).filter(
+            DagRun.dag_id.in_(existing_dag_ids),
+            DagRun.state == State.RUNNING,  # pylint: 
disable=comparison-with-callable
+            DagRun.external_trigger.is_(False)
+        ).group_by(DagRun.dag_id).all())

Review comment:
       I wonder if it should be done in this method.  This method only talks 
about data synchronization, not updating other fields. See: 
https://github.com/apache/airflow/pull/10956#discussion_r489347417




----------------------------------------------------------------
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:
[email protected]


Reply via email to