potiuk commented on a change in pull request #20975: URL: https://github.com/apache/airflow/pull/20975#discussion_r798633372
########## 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: I tihnk it would be much more performant (to be tested) if we create the table without indexes and add indexes after we rename the table (both primary keys and secondary indexes). I am afraid XCom table might be rather big in many cases so **any** optimization counts here. And from the experience - inserting a lot of (even bulk) data when index is there is a lot slower because the index will have to be rebuilt or re-shuffleed multiple times during the insert when it ougrows itself. On the other hand, when you create an index when all rows are there, the index will be created once with the right size/dimensions. The only worry is whether the primary key will be unique , but I believe this is guaranteed by the way how we construct the rows from existing data (but it would fail anyway just a bit earlier in this case). From the experience that might bring even 2x, 3x times improvement on large datasets (especially when we have multiple indexes like in this case) -- 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]
