GaneshPatil7517 opened a new pull request, #60307:
URL: https://github.com/apache/airflow/pull/60307
## What does this PR do?
Adds database indexes on `task_instance.dag_version_id` and
`dag_run.created_dag_version_id` to fix the slow performance of `airflow db
clean -t dag_version` command.
## Why is this needed?
When running `airflow db clean -t dag_version --batch-size 1000`, the
cleanup process was taking ~6 minutes per batch on tables with 300K+ rows. The
root cause was missing indexes on the foreign key columns that reference
`dag_version.id`.
The cleanup code in `db_cleanup.py` defines `dag_version` with
`dependent_tables=["task_instance", "dag_run"]`, meaning every delete operation
needs to check both tables for FK violations. Without indexes, PostgreSQL
performs full table scans.
## What changed?
1. **New migration**
(`0098_3_2_0_add_dag_version_id_indexes_for_db_cleanup.py`):
- Adds `idx_task_instance_dag_version_id` on
`task_instance(dag_version_id)`
- Adds `idx_dag_run_created_dag_version_id` on
`dag_run(created_dag_version_id)`
2. **Updated ORM models**: Added indexes to `__table_args__` for schema
consistency
## Performance Impact
| Scenario | Before | After |
|----------|--------|-------|
| `db clean -t dag_version` (300K rows, batch=1000) | ~6 min/batch | < 2 min
total |
Fixes #60145
--
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]