Im-joongyeon opened a new pull request, #77725: URL: https://github.com/apache/airflow/pull/77725
The triggerer runs `Trigger.clean_unused()` on every loop, and `_delete_unused_batch()` picks unreferenced triggers with the correlated `NOT EXISTS` subqueries that `~Trigger.task_instance.has()` and `~Trigger.assets.any()` generate. On MySQL 8.0 the optimizer turns the `task_instance` check into a materialized subquery that reads **every** entry of `ti_trigger_id`, including the NULLs of all task instances that never deferred. As a result, each cleanup reads the whole `task_instance` index, and that cost grows with history rather than with the number of deferred tasks. The asset check also joins `asset`, but `asset_watcher.asset_id` is a cascading FK to `asset.id`, so that join cannot change the result. This replaces both with uncorrelated `NOT IN` subqueries over the non-NULL `trigger_id`s, so the database range-scans only the rows that hold a trigger. It also drops the redundant `asset` join. Two details that the diff alone doesn't explain: - **The `IS NOT NULL` filters are needed for correctness as well as speed.** A single NULL in a `NOT IN` list makes the predicate match nothing. - **Both statements set `include_all_attempts=True`.** Naming `TaskInstance` in a FROM clause brings in the session's `working_set IS TRUE` filter, which `.has()` was never subject to. Without the opt-out, a trigger still held by an archived attempt would be deleted, and `task_instance_trigger_id_fkey` (`ON DELETE CASCADE`) would delete that attempt with it. #### Plans (2M `task_instance` rows, 1,000 deferred; 2,000 triggers, 500 unreferenced; fresh stats) MySQL 8.0.46: | | `task_instance` access | `asset` access | Query time | |---|---|---|---| | Before | `Covering index scan on task_instance using ti_trigger_id` (rows=2e+6) | `Covering index scan on asset using idx_asset_uri` | 219 ms | | After | `Covering index range scan on task_instance using ti_trigger_id over (NULL < trigger_id)` (rows=1000) | none | 1.27 ms | PostgreSQL 15: the planner already picked index-based anti joins for these data shapes, so this isn't a regression fix there. After the change it runs one hashed `Index Only Scan using ti_trigger_id` with `Index Cond: (trigger_id IS NOT NULL)` and no longer touches `asset` (0.98 ms → 0.53 ms; 9.4 ms → 5.9 ms with 24k triggers). SQLite: `SEARCH task_instance USING COVERING INDEX ti_trigger_id (trigger_id=?)` evaluated once per trigger becomes a single `(trigger_id>?)` list subquery. The new plan test asserts this. #### Tests run - `airflow-core/tests/unit/models/test_trigger.py`: all passing on SQLite (103 tests) and PostgreSQL 15 (101 passed, 2 SQLite-only skipped). - `breeze run --backend mysql pytest airflow-core/tests/unit/models/test_trigger.py airflow-core/tests/unit/jobs/test_triggerer_job.py`: 446 passed, 3 skipped (the 2 SQLite-only plan tests and one test that was already skipped on MySQL). - `test_delete_unused_batch_reads_only_task_instances_holding_a_trigger` fails on `main`. - `test_delete_unused_batch_keeps_trigger_held_by_archived_attempt` fails if `include_all_attempts=True` is removed from both statements. - `prek run --stage pre-commit` on the changed files, including `mypy-airflow-core`. --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes — Claude Code (Opus 5.5) Generated-by: Claude Code (Opus 5.5) 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]
