Skip to content

Commit

Permalink
Fix slow DAG deletion due to missing dag_id index for job table (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kushsharma authored Dec 30, 2021
1 parent 6d25d63 commit ac9f29d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions airflow/jobs/base_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class BaseJob(Base, LoggingMixin):
__table_args__ = (
Index('job_type_heart', job_type, latest_heartbeat),
Index('idx_job_state_heartbeat', state, latest_heartbeat),
Index('idx_job_dag_id', dag_id),
)

task_instances_enqueued = relationship(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# 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.

"""adding index for dag_id in job
Revision ID: 587bdf053233
Revises: f9da662e7089
Create Date: 2021-12-14 10:20:12.482940
"""

from alembic import op

# revision identifiers, used by Alembic.
revision = '587bdf053233'
down_revision = 'f9da662e7089'
branch_labels = None
depends_on = None


def upgrade():
"""Apply adding index for dag_id in job"""
op.create_index('idx_job_dag_id', 'job', ['dag_id'], unique=False)


def downgrade():
"""Unapply adding index for dag_id in job"""
op.drop_index('idx_job_dag_id', table_name='job')
5 changes: 3 additions & 2 deletions docs/apache-airflow/migrations-ref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Here's the list of all the Database Migrations that are executed via when you ru
+--------------------------------+------------------+-----------------+---------------------------------------------------------------------------------------+
| Revision ID | Revises ID | Airflow Version | Description |
+--------------------------------+------------------+-----------------+---------------------------------------------------------------------------------------+
| ``f9da662e7089`` (head) | ``786e3737b18f`` | ``2.3.0`` | Add ``LogTemplate`` table to track changes to config values ``log_filename_template`` |
| | | | and ``task_log_prefix_template``. |
| ``587bdf053233`` (head) | ``f9da662e7089`` | ``2.3.0`` | Add index for ``dag_id`` column in ``job`` table. |
+--------------------------------+------------------+-----------------+---------------------------------------------------------------------------------------+
| ``f9da662e7089`` | ``786e3737b18f`` | ``2.3.0`` | Add ``LogTemplate`` table to track changes to config values ``log_filename_template`` |
+--------------------------------+------------------+-----------------+---------------------------------------------------------------------------------------+
| ``786e3737b18f`` | ``5e3ec427fdd3`` | ``2.3.0`` | Add ``timetable_description`` column to DagModel for UI. |
+--------------------------------+------------------+-----------------+---------------------------------------------------------------------------------------+
Expand Down

0 comments on commit ac9f29d

Please sign in to comment.