【问题标题】:Performance problem of MERGE statement in Azure SynapseAzure Synapse 中 MERGE 语句的性能问题
【发布时间】:2022-11-10 14:40:24
【问题描述】:

我的 DWH 部署在 Azure Synapse SQL 池上。 我通过包含更新、插入和删除(u-i-d)操作的脚本将数据加载到 DWH。近 5000 万行的目标表完全加载持续时间为 12 分钟。
最近我尝试使用 MERGE 语句而不是 u-i-d。而且我发现 MERGE 的性能比 u-i-d 差得多 - MERGE 需要 1 小时,而 u-i-d 需要 12 分钟!

请分享您对 Azure 突触的 MERGE 语句的体验,朋友们! 在 Synapse 中 MERGE 真的比单独的更新-插入-删除操作更糟糕吗?

【问题讨论】:

    标签: sql merge azure-synapse


    【解决方案1】:

    按照女士文档MERGE (Transact-SQL) - SQL Server | Microsoft Learn 上,Merge 语句更适用于复杂语句,而对于使用 Insert、Update 和 delete 语句合并的简单活动效果更好。

    当两个表具有复杂的匹配特征混合时,为 MERGE 语句描述的条件行为最有效。例如,如果行不存在则插入行,或者如果匹配行则更新行。当简单地根据另一个表的行更新一个表时,使用基本的 INSERT、UPDATE 和 DELETE 语句来提高性能和可伸缩性。

    • 我尝试使用简单的语句重现和比较这两种方法。
    • 示例表如下图所示。

    • Merge 语句用于合并,耗时 9 秒
    MERGE Products AS  TARGET
    
    USING UpdatedProducts AS  SOURCE
    
    ON (TARGET.ProductID =  SOURCE.ProductID)
    
    --When records are matched, update the records if there is any change
    
    WHEN  MATCHED  AND  TARGET.ProductName <>  SOURCE.ProductName OR  TARGET.Rate <>  SOURCE.Rate
    
    THEN  UPDATE  SET  TARGET.ProductName =  SOURCE.ProductName, TARGET.Rate =  SOURCE.Rate
    
    --When no records are matched, insert the incoming records from source table to target table
    
    WHEN  NOT  MATCHED  BY  TARGET
    
    THEN  INSERT (ProductID, ProductName, Rate) VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)
    
    --When there is a row that exists in target and same record does not exist in source then delete this record target
    
    WHEN  NOT  MATCHED  BY  SOURCE
    
    THEN  DELETE ;
    

    • 然后尝试使用更新、插入和删除语句。花了将近0秒。

    更新、插入和删除更适合简单的场景。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2021-04-24
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      相关资源
      最近更新 更多