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

   `ti_run` eager-loads `dag_run.consumed_asset_events`, but serialising the 
run context then reads `event.asset` and `event.source_aliases` on each event, 
and those load lazily: two `SELECT`s per consumed event, inside the task 
instance's `FOR UPDATE` transaction, on every task start. Measured with 
`CountQueries` on Postgres: 9 queries with one consumed event, 47 with twenty.
   
   `/execution/asset-events/by-asset` and `/by-asset-alias` have the same shape 
one level down: `created_dagruns` loads lazily per event, and `limit` defaults 
to none, so a task reading `inlet_events` pays one query per event ever 
recorded for the asset. Measured: 5 queries for 3 events, 32 for 30.
   
   ### What changed
   
   - `ti_run`: nest `joinedload(AssetEvent.asset)` and 
`selectinload(AssetEvent.source_aliases)` under the existing 
`joinedload(DR.consumed_asset_events)`. Same shape #65422 used to preload 
`source_aliases` on the executor-events path.
   - `_get_asset_events_through_sql_clauses`: add 
`joinedload(AssetEvent.asset)` and `selectinload(AssetEvent.created_dagruns)`, 
which is what the public API already does for these rows in 
`core_api/routes/public/assets.py`. One option set serves both endpoints: the 
by-asset path keeps its filter join on `AssetEvent.asset` and the loader adds 
its own aliased join, while the by-alias path filters through `source_aliases`.
   
   Responses are unchanged; only the number of statements changes.
   
   ### Tests
   
   - Two regression tests assert a request costs the same number of queries at 
1 vs 5 consumed events and at 3 vs 12 asset events. Both fail on main (`14 == 
5` for by-asset) and pass with the change.
   - Full `test_task_instances.py` and `test_asset_events.py` pass on Postgres 
via Breeze (259 passed, 1 skipped), as do the CI-selected test types `Always`, 
`API` and `Providers[common.compat,fab]`.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes — Claude Code (Fable 5.1)
   
   Generated-by: Claude Code (Fable 5.1) 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]

Reply via email to