首页 > 解决方案 > Azure 数据工厂 V2:用于 SQL 合并的复制或存储过程活动

问题描述

我们的 Azure 数据工厂 v2 解决方案中有许多数据库表合并步骤。我们在 Azure SQL Server DB 的单个实例中合并表。源表和目标表位于不同的数据库模式中。源被定义为对单个表的选择或两个表的连接。

我怀疑从性能的角度来看,下面描述的哪种场景更好。

场景一(每桌)

存储过程活动调用执行所有工作的存储过程。管道中的存储过程活动调用该存储过程。使用所有源数据更新目标表。这种存储过程的一个例子:

create or alter procedure dwh.fill_lnk_cemvypdet_cemstr2c_table_with_stage_data as
    merge
        dwh.lnk_cemvypdet_cemstr2c as target
    using

        (select
                t.sa_hashkey cemvypdet_hashkey,
                t.sa_timestamp load_date,
                t.sa_source record_source,
                d.sa_hashkey cemstr2c_hashkey
            from
                egje.cemvypdet t
            join
                egje.cemstr2c d
            on
                t.id_mstr = d.id_mstr)
        as source
        on target.cemvypdet_hashkey = source.cemvypdet_hashkey
            and target.cemstr2c_hashkey = source.cemstr2c_hashkey
        when not matched then
            insert(
                cemvypdet_hashkey,
                cemstr2c_hashkey,
                record_source,
                load_date,
                last_seen_date)
            values(
                source.cemvypdet_hashkey,
                source.cemstr2c_hashkey,
                source.record_source,
                source.load_date,
                source.load_date)
        when matched then
            update set last_seen_date = source.load_date;

场景二(每行)

Copy 活动声明要在 Target 选项卡中调用的存储过程,以便该活动为源的每一行调用存储过程。

create or alter procedure dwh.fill_lnk_cemvypdet_cemstr2c_subset_table_row_with_stage_data
@lnk_cemvypdet_cemstr2c_subset dwh.lnk_cemvypdet_cemstr2c_subset_type readonly
as
    merge
        dwh.lnk_cemvypdet_cemstr2c_subset as target
    using

    @lnk_cemvypdet_cemstr2c_subset
        as source
        on target.cemvypdet_hashkey = source.cemvypdet_hashkey
            and target.cemstr2c_hashkey = source.cemstr2c_hashkey
        when not matched then
            insert(
                hashkey,
                cemvypdet_hashkey,
                cemstr2c_hashkey,
                record_source,
                load_date,
                last_seen_date)
            values(
                source.hashkey,
                source.cemvypdet_hashkey,
                source.cemstr2c_hashkey,
                source.record_source,
                source.load_date,
                source.load_date)
        when matched then
            update set last_seen_date = source.load_date;

@lnk_cemvypdet_cemstr2c_subset 类型定义为遵循目标表结构的表类型。

标签: azurestored-proceduresazure-sql-databaseazure-data-factoryazure-data-factory-2

解决方案


场景 1 应该有更好的性能,但考虑到以下优化:

  1. 在源表中的唯一且覆盖的连接列上创建索引。
  2. 在目标表中的连接列上创建唯一聚集索引。
  3. 参数化 ON 子句和 WHEN 子句中的所有文字值。
  4. 通过使用 OFFSET 和 ROWS FETCH NEXT 或通过在源或目标上定义返回过滤行并将视图作为源或目标表引用的视图,将数据子集从源表合并到目标表。此外,不建议使用 TOP 子句的 WITH 子句从源表或目标表中过滤掉行,因为它们会产生不正确的结果。
  5. 要进一步优化合并操作,请尝试不同的批量大小。就是原因。

推荐阅读