seanghaeli commented on code in PR #77725:
URL: https://github.com/apache/airflow/pull/77725#discussion_r4731167507
##########
airflow-core/src/airflow/models/trigger.py:
##########
@@ -370,8 +370,25 @@ def clean_unused(cls, *, session: Session = NEW_SESSION)
-> None:
@classmethod
def _delete_unused_batch(cls, batch_size: int, *, session: Session) ->
None:
- unreferenced = (~cls.assets.any(), ~cls.callback.has(),
~cls.task_instance.has())
- ids =
select(cls.id).where(*unreferenced).order_by(cls.id).limit(batch_size)
+ from airflow.models.callback import Callback # to avoid circular
import: Callback -> Trigger
+
+ # Uncorrelated NOT IN over only the non-NULL trigger_ids: MySQL
materializes the correlated NOT EXISTS
+ # from ``.has()`` by scanning every row of ti_trigger_id, NULLs
included, on each cleanup. The
+ # IS NOT NULL filters are also required for correctness: a single NULL
makes NOT IN match nothing.
+ # Archived attempts still count as references (the FK would
cascade-delete them with the trigger), so
+ # both statements opt out of the session's ``working_set`` filter, as
``.has()`` was never subject to it.
+ unreferenced = (
+ cls.id.not_in(select(AssetWatcherModel.trigger_id)),
+
cls.id.not_in(select(Callback.trigger_id).where(Callback.trigger_id.is_not(None))),
+
cls.id.not_in(select(TaskInstance.trigger_id).where(TaskInstance.trigger_id.is_not(None))),
Review Comment:
On MySQL this gets a lot slower once there are many deferred tasks. In
Breeze (MySQL 8.0.46) with 131k deferred and 262k finished task instances,
`Trigger.clean_unused()` took about 155 s per call on this PR against about 2 s
on main, and since it runs on every triggerer loop the triggerer stopped
heartbeating for a couple of minutes. MySQL plans this `NOT IN` as a
per-trigger `EXISTS` here; the same rewrite in #77738, which has no `ORDER BY
... LIMIT`, stays fast. Could we find a form MySQL doesn't plan this way, or
keep `~cls.task_instance.has()` for task instances?
--
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]