StruckGuide8154 opened a new pull request, #64928:
URL: https://github.com/apache/airflow/pull/64928
Optimize API N+1 queries in TI and Grid endpoints
High database latency was observed in the TaskInstance listing and
Grid UI components due to N+1 query patterns during serialization.
Specifically, for each TaskInstance in a deferred state, the ORM
issued individual queries for the associated Trigger and Job models.
In the Grid view, the NDJSON stream was executing a separate database
lookup for each DAG run requested, resulting in linear query scaling
relative to the number of grid columns.
This PR implements the following technical optimizations:
- Implement `joinedload` for Trigger and TriggererJob in the
`eager_load_TI_and_TIH_for_validation` helper to consolidate
relationship fetching.
- Batch TaskInstance retrieval in the Grid NDJSON stream using
`run_id.in_(run_ids)` and `itertools.groupby` to reduce database
round-trips from O(N) to O(1).
- Align `get_dag_run` detail route optimization with the existing
list-view eager loading strategy for consistency.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes — Gemini Flash
Generated-by: Gemini Flash following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
--
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]