Skip to content

perf: add indexes on serialized_dag, dag_code, and task_instance - #64311

Closed
sortega wants to merge 1 commit into
apache:mainfrom
DataDog:sortega/perf-add-metadb-indexes
Closed

sortega wants to merge 1 commit into
apache:mainfrom
DataDog:sortega/perf-add-metadb-indexes

Conversation

@sortega

@sortega sortega commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Add missing database indexes to improve query performance on the metadata DB:

  • serialized_dag(dag_id, last_updated) — speeds up queries fetching the latest serialized DAG per dag_id
  • dag_code(dag_id) — speeds up DAG code lookups by dag_id
  • task_instance(dag_version_id) — speeds up joins/filters on the dag_version_id FK (added in migration 0047 without an index)

These indexes resolve full table scan issues observed in production under load. Supersedes #60886 which only covered the task_instance.dag_version_id index.

closes #60883


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 4.6)

Generated-by: Claude Code (Opus 4.6) following the guidelines

@sortega
sortega marked this pull request as ready for review March 27, 2026 17:27

@kalluripradeep kalluripradeep left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good set of indexes. Worth noting that on large deployments, running this migration against a big task_instance table could take a while and potentially lock it. Might be worth a heads-up in the migration notes so operators can plan for it.

@sortega
sortega force-pushed the sortega/perf-add-metadb-indexes branch 2 times, most recently from 00054ac to 3f62158 Compare March 30, 2026 07:30
@sortega

sortega commented Mar 30, 2026

Copy link
Copy Markdown
Contributor Author

@kalluripradeep that's a good point. Added the warning to the newsfragment

@sortega
sortega force-pushed the sortega/perf-add-metadb-indexes branch from 3f62158 to b32d3a8 Compare April 1, 2026 09:34

@ephraimbuddy ephraimbuddy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@sortega
sortega force-pushed the sortega/perf-add-metadb-indexes branch from b32d3a8 to 79e6a07 Compare April 1, 2026 13:59
@kaxil
kaxil requested review from Copilot and removed request for kalluripradeep April 2, 2026 00:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds missing metadata DB indexes to reduce full table scans and speed up common lookups/joins involving serialized DAGs, DAG code, and task instances.

Changes:

  • Adds new Alembic migration to create indexes on serialized_dag(dag_id, last_updated), dag_code(dag_id), and task_instance(dag_version_id).
  • Updates ORM models (SerializedDagModel, DagCode, TaskInstance) to declare the same indexes.
  • Updates the migration head mapping for Airflow 3.3.0.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
airflow-core/src/airflow/utils/db.py Bumps the 3.3.0 migration head to the new revision.
airflow-core/src/airflow/models/taskinstance.py Declares an index on dag_version_id at the ORM level.
airflow-core/src/airflow/models/serialized_dag.py Declares a composite index on (dag_id, last_updated) at the ORM level.
airflow-core/src/airflow/models/dagcode.py Declares an index on dag_id at the ORM level.
airflow-core/src/airflow/migrations/versions/0110_3_2_0_add_performance_indexes.py New migration that creates/drops the indexes (with MySQL downgrade exception for the TI FK index).
airflow-core/newsfragments/64311.improvement.rst Newsfragment documenting the performance improvement.

@sortega
sortega force-pushed the sortega/perf-add-metadb-indexes branch 2 times, most recently from 4c1c008 to 4c5167f Compare April 2, 2026 07:57
@hussein-awala
hussein-awala requested a review from Copilot April 3, 2026 08:28
@hussein-awala hussein-awala added this to the Airflow 3.3.0 milestone Apr 3, 2026

@hussein-awala hussein-awala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, thanks @sortega for the improvement!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

@sortega
sortega force-pushed the sortega/perf-add-metadb-indexes branch from 4c5167f to f093fb7 Compare April 3, 2026 09:04
@hussein-awala

Copy link
Copy Markdown
Member

@sortega could you please add this index to the ignore list in the in_sync test for DB models?

@sortega
sortega force-pushed the sortega/perf-add-metadb-indexes branch from f093fb7 to 5643f65 Compare April 6, 2026 07:40
@sortega

sortega commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

Added idx_task_instance_dag_version_id to the ignore list in test_db.py — the ORM always declares the index but the migration skips it on MySQL (where the FK auto-creates one), so the in_sync test sees a mismatch.

Add missing database indexes to improve query performance:
- serialized_dag(dag_id, last_updated): speeds up fetching latest serialized DAG
- dag_code(dag_id): speeds up DAG code lookups by dag_id
- task_instance(dag_version_id): speeds up joins/filters on dag_version_id FK

These indexes resolve full table scan issues observed in production when
querying the metadata database under load.
@sortega
sortega force-pushed the sortega/perf-add-metadb-indexes branch from 5643f65 to 52e58ad Compare April 6, 2026 07:43
@potiuk

potiuk commented Apr 6, 2026 •

Copy link
Copy Markdown
Member

@sortega A few things need addressing before review — see our Pull Request quality criteria.

  • ❌ Other failing CI checks: Failing: MySQL tests: core / DB-core:MySQL:8.0:3.10:Core...Serialization. Run prek run --from-ref main locally to reproduce. See static checks docs.

No rush.


Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

@kaxil
kaxil requested a review from Copilot April 10, 2026 19:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment on lines +183 to 186
# MySQL auto-creates an index on FK columns; the migration skips creating this
# index on MySQL to avoid a duplicate, but the ORM always declares it.
lambda t: t[0] == "add_index" and t[1].name == "idx_task_instance_dag_version_id",
]

Copilot AI Apr 10, 2026

Copy link

Choose a reason for hiding this comment

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

The new ignore for idx_task_instance_dag_version_id is applied unconditionally, which means this schema-sync test would also ignore a missing/renamed dag_version_id index on non-MySQL backends. Consider only adding this ignore when the active dialect is MySQL (where the migration intentionally skips creating the named index).

Suggested change
# MySQL auto-creates an index on FK columns; the migration skips creating this
# index on MySQL to avoid a duplicate, but the ORM always declares it.
lambda t: t[0] == "add_index" and t[1].name == "idx_task_instance_dag_version_id",
]
]
if settings.engine.dialect.name == "mysql":
# MySQL auto-creates an index on FK columns; the migration skips creating this
# index on MySQL to avoid a duplicate, but the ORM always declares it.
ignores.append(lambda t: t[0] == "add_index" and t[1].name == "idx_task_instance_dag_version_id")

Copilot uses AI. Check for mistakes.
@potiuk
potiuk marked this pull request as draft April 22, 2026 15:11
@potiuk

potiuk commented Apr 22, 2026 •

Copy link
Copy Markdown
Member

@sortega This PR was triaged 16 days ago with no reply since. Converting to draft — this PR doesn't yet meet our Pull Request quality criteria.

  • ❌ Merge conflicts: this branch has conflicts with main and cannot be merged as-is. Rebase onto the current main to resolve: git fetch upstream main && git rebase upstream/main, resolve, force-push.
  • ❌ Failing CI: MySQL tests: core / DB-core:MySQL:8.0:3.10:Core...Serialization is failing. Reproduce locally with breeze testing core-tests --backend mysql --test-type Core and investigate.
  • ❌ Unresolved review comments (1 thread): please walk through each unresolved review thread. Even if a suggestion looks incorrect or irrelevant — and some of them will be, especially any comments left by automated reviewers like GitHub Copilot — it is still the author's responsibility to respond: apply the fix, reply in-thread with a brief explanation of why the suggestion does not apply, or resolve the thread if the feedback is no longer relevant. Leaving threads unaddressed for weeks blocks the PR from moving forward.

See the linked criteria for how to fix each item, then mark the PR "Ready for review". This is not a rejection — just an invitation to bring the PR up to standard. No rush.


Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

@potiuk

potiuk commented Apr 22, 2026 •

Copy link
Copy Markdown
Member

Quick follow-up to the triage comment above — one clarification on the "Unresolved review comments" item:

Once you believe a thread has been addressed — whether by pushing a fix, or by replying in-thread with an explanation of why the suggestion doesn't apply — please mark the thread as resolved yourself by clicking the "Resolve conversation" button at the bottom of each thread. Reviewers don't auto-close their own threads, so an addressed-but-unresolved thread reads as "still waiting on the author" and keeps the PR from moving forward. The author doing the resolve-click is the expected convention on this project.


Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

@ColtenOuO

Copy link
Copy Markdown
Contributor

Hi @sortega, thanks for picking this up — the fix shape looks right and the maintainer reviews are very encouraging.

I noticed the branch hasn't had activity in about a month and there are a few CI failures plus merge conflicts to resolve. Are you still planning to finish this, or would you welcome help? I'm happy to take any of these on.

@potiuk

potiuk commented May 5, 2026

Copy link
Copy Markdown
Member

@sortega This draft PR has been inactive for 13 days since the last triage comment and no response from the author. Closing to keep the queue clean.

You are welcome to reopen this PR when you resume work, or to open a new one addressing the issues previously raised. There is no rush — take your time.


Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid full table scan on /structure/{dag_id}

7 participants