Skip to content

Conversation

@tirkarthi
Copy link
Contributor

The recent dag runs query was triggering access to task_instances, task_instance_histories, deadlines and dag_run_note tables as part of model_validate using DagRunResponse and each attribute access caused a query resulting n+1 queries for each dagrun in the recent runs response. The recent runs response included a lot of fields that are unused in the UI. This PR simplifies the response to selectively query the fields and uses DAGRunLightResponse to include duration field which fulfills all the fields required in the frontend.

Additionally, the pending_actions filter to filter by required actions had condition as has_pending_actions.value is not False and in case of normal dags list page the has_pending_actions.value is None and thus None is not False also lead to evaluating query where it was not required. This also improved the performance.

With the removal of n+1 queries and unused fields the overall response was significantly improved up to 7-8x where page that took 1s second to load for 15 dags and 2100 dagruns now loaded in around 100ms. This is also more noticeable in large environments where the n+1 queries are not executed. The http response size was also reduced with removal of unused fields in the response.

pydantic/pydantic#8192
sqlalchemy/sqlalchemy#10120
https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.noload

Closes #57418

@boring-cyborg boring-cyborg bot added area:API Airflow's REST/HTTP API area:UI Related to UI/UX. For Frontend Developers. labels Oct 28, 2025
Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, we were probably missing some loading options in the queries, and if we don't need them.

Just one question regarding backward compatibility, otherwise LGTM.

@pierrejeambrun
Copy link
Member

pierrejeambrun commented Oct 28, 2025

What I plan to do, and we could do here is add some assert_queries_count in tests for get_entities so we can control the query explosion if someone do a bad modification or forget some eager loading options.

@pierrejeambrun pierrejeambrun added this to the Airflow 3.1.2 milestone Oct 28, 2025
@pierrejeambrun pierrejeambrun added the backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch label Oct 28, 2025
Copy link
Contributor

@bbovenzi bbovenzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catches!

@pierrejeambrun pierrejeambrun merged commit f722f3c into apache:main Oct 28, 2025
115 checks passed
github-actions bot pushed a commit that referenced this pull request Oct 28, 2025
…ponse model. (#57425)

* Use DAGRunLightResponse to stop loading unused fields in response that trigger n+1 queries to other tables.

* Update frontend type and tests.

* Fix MyPy error.
(cherry picked from commit f722f3c)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
@github-actions
Copy link

Backport successfully created: v3-1-test

Status Branch Result
v3-1-test PR Link

pierrejeambrun pushed a commit that referenced this pull request Oct 28, 2025
…ponse model. (#57425) (#57444)

* Use DAGRunLightResponse to stop loading unused fields in response that trigger n+1 queries to other tables.

* Update frontend type and tests.

* Fix MyPy error.
(cherry picked from commit f722f3c)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Lzzz666 pushed a commit to Lzzz666/airflow that referenced this pull request Oct 30, 2025
…apache#57425)

* Use DAGRunLightResponse to stop loading unused fields in response that trigger n+1 queries to other tables.

* Update frontend type and tests.

* Fix MyPy error.
@ephraimbuddy ephraimbuddy added the type:bug-fix Changelog: Bug Fixes label Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:API Airflow's REST/HTTP API area:UI Related to UI/UX. For Frontend Developers. backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

n+1 queries in ui/dags endpoint used by dags list page causing performance issues

4 participants