potiuk commented on a change in pull request #20975: URL: https://github.com/apache/airflow/pull/20975#discussion_r798648573
########## File path: airflow/migrations/versions/c306b5b5ae4a_switch_xcom_table_to_use_run_id.py ########## @@ -0,0 +1,139 @@ +# +# 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. + +"""Switch XCom table to use ``run_id``. + +Revision ID: c306b5b5ae4a +Revises: e655c0453f75 +Create Date: 2022-01-19 03:20:35.329037 +""" +from typing import Sequence + +from alembic import op +from sqlalchemy import Column, Index, Integer, LargeBinary, MetaData, Table, select + +from airflow.migrations.db_types import TIMESTAMP, StringID + +# Revision identifiers, used by Alembic. +revision = "c306b5b5ae4a" +down_revision = "e655c0453f75" +branch_labels = None +depends_on = None + + +metadata = MetaData() + +dagrun = Table( + "dag_run", + metadata, + Column("id", Integer, primary_key=True), + Column("dag_id", StringID(), nullable=False), + Column("run_id", StringID(), nullable=False), + Column("execution_date", TIMESTAMP, nullable=False), +) + + +def _get_new_xcom_columns() -> Sequence[Column]: + return [ + Column("dagrun_id", Integer(), nullable=False, primary_key=True), + Column("task_id", StringID(), nullable=False, primary_key=True), + Column("key", StringID(length=512), nullable=False, primary_key=True), + Column("value", LargeBinary), + Column("timestamp", TIMESTAMP, nullable=False), + Column("dag_id", StringID(), nullable=False), + Column("run_id", StringID(), nullable=False), + ] + + +def _get_old_xcom_columns() -> Sequence[Column]: + return [ + Column("key", StringID(length=512), nullable=False, primary_key=True), + Column("value", LargeBinary), + Column("timestamp", TIMESTAMP, nullable=False, primary_key=True), + Column("task_id", StringID(), nullable=False, primary_key=True), + Column("dag_id", StringID(), nullable=False, primary_key=True), + Column("execution_date", StringID(), nullable=False, primary_key=True), + ] + + +def upgrade(): + """Switch XCom table to use run_id. + + For performance reasons, this is done by creating a new table with needed + data pre-populated, adding back constraints we need, and renaming it to + replace the existing XCom table. + """ + op.create_table( + "__airflow_tmp_xcom", + *_get_new_xcom_columns(), + Index("idx_xcom_key", "key"), Review comment: Example stack overflow case: https://stackoverflow.com/questions/350003/bulk-insert-with-or-without-index where - there an example where 20 hours operation dropped to 1h 25m - but it's also worth noting that (depending on DB and ordering) it might or might not be helping. But looking at the way how our indexes are, I am quite confident it will help a lot. -- 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]
