potiuk commented on code in PR #27725:
URL: https://github.com/apache/airflow/pull/27725#discussion_r1025861242


##########
airflow/jobs/scheduler_job.py:
##########
@@ -774,25 +775,46 @@ def _execute(self) -> None:
     @provide_session
     def _update_dag_run_state_for_paused_dags(self, session: Session = 
NEW_SESSION) -> None:
         try:
-            paused_dag_ids = DagModel.get_all_paused_dag_ids()
-            for dag_id in paused_dag_ids:
-                if dag_id in self._paused_dag_without_running_dagruns:
-                    continue
+            paused_dag_ids = {
+                paused_dag_id
+                for paused_dag_id, in session.query(DagModel.dag_id)
+                .join(DagRun.dag_model)
+                .filter(
+                    DagModel.is_paused == expression.true(),
+                    DagRun.state == DagRunState.RUNNING,
+                    DagRun.run_type != DagRunType.BACKFILL_JOB,
+                )
+                .all()
+            }
 
-                dag = SerializedDagModel.get_dag(dag_id)
-                if dag is None:
-                    continue
+            for dag_id in paused_dag_ids:

Review Comment:
   I think it can be further optimized. I believe we should be able to join the 
query above with the two below as single select query with selected DagRuns - 
all the conditions we run (even the last_scheduling_decision one) seem to be 
possible to add as a bit more complex SqlAlchemy filter.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to