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

   ## Why
   
   - `GET /api/v2/dags/{dag_id}/dagRuns/{dag_run_id}` returns `dag_versions`, 
read from `DagRun.dag_versions`.
   - Without a prefetch, that property walks `task_instances` and 
`task_instances_histories` and loads every row, only to read `dag_version_id`.
   - The list endpoints and the grid already avoid this with 
`attach_dag_versions_to_runs()`. The single-run endpoint does not.
   - On a production deployment, this endpoint takes 4-5.5 s for runs with many 
mapped task instances, most of it in the task instance `SELECT` and the ORM 
load.
   
   ## How
   
   - Load the run with `eager_load_dag_run_for_list()`, like the list endpoints 
(it already includes the team eager load).
   - Call `attach_dag_versions_to_runs([dag_run])` before returning.
   - The response is unchanged.
   
   Related: #71459 makes the same `get_dag_run` eager-load change and also 
moves the DISTINCT query into `DagRun.dag_versions` itself. This PR is the 
minimal fix for the endpoint only. Happy to close it if #71459 lands first.
   
   ## Results
   
   Local check on 3.3.2 (SQLite, one run with 2,000 task instances on 2 Dag 
versions):
   
   | Path | `dag_versions` | Queries | Time |
   |---|---|---|---|
   | Before | [1, 2] | 5 | 50 ms |
   | After | [1, 2] | 3 | 6 ms |
   
   ## Testing
   
   - New `test_get_dag_run_does_not_load_task_instances`: the response's 
`dag_versions` matches the ORM result, and no `TaskInstance` is loaded.
   - To do before review: run the `test_dag_run.py` suite in Breeze.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   _To be completed by the author before marking ready for review._
   
   - [ ] Yes (please specify the tool below)
   
   ---
   


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