kaxil opened a new pull request, #60957:
URL: https://github.com/apache/airflow/pull/60957

   ## Summary
   
   In `dags_needing_dagruns()`, the query loads `AssetDagRunQueue` records with 
`joinedload(dag_model)`, but then accesses `adrq.asset` without eager loading 
when building the `dag_statuses` dict. This triggers a separate query for each 
ADRQ record (N+1).
   
   ```python
   # Line 643 accesses adrq.asset without eager loading
   dag_statuses: dict[str, dict[UKey, bool]] = {
       dag_id: {SerializedAssetUniqueKey.from_asset(adrq.asset): True for adrq 
in adrqs}
       for dag_id, adrqs in adrq_by_dag.items()
   }
   ```
   
   ## Fix
   
   Add `joinedload(AssetDagRunQueue.asset)` to load assets in a single joined 
query:
   
   ```python
   for adrq in session.scalars(
       select(AssetDagRunQueue).options(
           joinedload(AssetDagRunQueue.dag_model),
           joinedload(AssetDagRunQueue.asset),  # <-- Added
       )
   ):
   ```
   
   ## Test
   
   Added `test_dags_needing_dagruns_query_count` to verify query count stays 
constant regardless of ADRQ record count, preventing N+1 regression.


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