-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Save scheduler execution time by adding new Index idea for dag_run #30827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
potiuk
merged 3 commits into
apache:main
from
boschglobal:feature/optimize-scheduler-run-time-4
May 19, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
airflow/migrations/versions/0126_2_7_0_add_index_on_last_scheduling_decision_.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """Add index on last_scheduling_decision NULLS FIRST, execution_date, state for queued dagrun | ||
|
|
||
| Revision ID: 14db5484317e | ||
| Revises: 937cbd173ca1 | ||
| Create Date: 2023-05-14 21:16:42.399167 | ||
|
|
||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| from alembic import op | ||
| from sqlalchemy import text | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "14db5484317e" | ||
| down_revision = "937cbd173ca1" | ||
| branch_labels = None | ||
| depends_on = None | ||
| airflow_version = "2.7.0" | ||
|
|
||
|
|
||
| def upgrade(): | ||
| """Apply Add index on last_scheduling_decision NULLS FIRST, execution_date, state for queued dagrun""" | ||
| conn = op.get_bind() | ||
| if conn.dialect.name == "postgresql": | ||
| with op.batch_alter_table("dag_run") as batch_op: | ||
| batch_op.create_index( | ||
| "idx_last_scheduling_decision_queued", | ||
| [text("last_scheduling_decision NULLS FIRST"), "execution_date", "state"], | ||
| postgresql_where=text("state='queued'"), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| """Unapply Add index on last_scheduling_decision NULLS FIRST, execution_date, state for queued dagrun""" | ||
| conn = op.get_bind() | ||
| if conn.dialect.name == "postgresql": | ||
| with op.batch_alter_table("dag_run") as batch_op: | ||
| batch_op.drop_index("idx_last_scheduling_decision_queued") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 4987842fd67d29e194f1117e127d3291ba60d3fbc3e81cba75ce93884c263321 | ||
| 811b1c45f8fa985feacacffafc30c82a6049bb33948c33bb218c13c48f971097 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting idea to add partial index here, Likely the improvment in your case is because the whole index takes too much memory of your server and gest swapped out. I do not know that much about thos partial indexes but it does seem like a legitimate optimisiation that might improve one of the most frequent queries scheduler runs,
I do not see an immediate side-effect of that one (and regarding the migration - I think you should follow the regular CONTRIBUTORS.rst alembic guide: https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#metadata-database-updates
but I think I'd love others to comment if they also see any problem with such an optimisation?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback! I moved the Index away from the dagrun.py because then the auto migration failes on the sqllite because not supports NULLS FIRST. Added the Index now only into the migration if not sqllite. Is this the way to go or did i missed something here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a bried look ad SQLAlchemy docs, would https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.Constraint.dialect_options help in this area? (=dialect_options) to have this specific condition in postgres only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do it this way but then - for consistency - we should change it everywhere else (because we are doing the same approach in multiple other places). I prefer this kind of explicit if actually.