长时间运行的SQL查询填充tempdb

长时间运行的SQL查询填充tempdb,sql,sql-server,tsql,azure-sql-database,Sql,Sql Server,Tsql,Azure Sql Database,我正在运行的存储过程在填充tempdb(32GB)时失败。大约一个小时后它就失效了 存储过程如下所示: /****** Object: StoredProcedure [Reporting].[Merge_Fact_Customer_Engagement] Script Date: 23/08/2019 14:32:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [Reportin

我正在运行的存储过程在填充tempdb(32GB)时失败。大约一个小时后它就失效了

存储过程如下所示:

/****** Object:  StoredProcedure [Reporting].[Merge_Fact_Customer_Engagement]    Script Date: 23/08/2019 14:32:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER   PROCEDURE [Reporting].[Merge_Fact_Customer_Engagement]
AS


IF OBJECT_ID('tempdb..#CustEng_stg') IS NOT NULL
    DROP TABLE #CustEng_stg;
IF OBJECT_ID('tempdb..#CustEng_delta') IS NOT NULL
    DROP TABLE #CustEng_delta;                                                                                      


SELECT

-- Business Key                                                                                                 
    [Customer_Engagement_Id]        

-- Foreign Keys                                         
    ,cec.[Customer_Engagement_Context_Key]                                                  -- Engagement_Type_Id                               
    ,d.[Date_Key]                           AS [Engagement_Date_Key]                        -- Engagement_Date                                  
    ,t.[TimeKey]                            AS [Engagement_Time_Key]                        -- Engagement_Time                                  
    ,c.[Contact_Key]                                                                        -- LBS_Number                                       
    ,age.[Age_Key]                          AS [Age_On_Engagement_Key]                      -- Age_On_Engagement                                
    ,pic.[Programme_Interest_Context_Key]                                                   -- Programme_Interest_Status_Code                   
    ,cnt1.[Country_Key]                     AS [Country_Of_Nationality_Key]                 -- Country_Of_Nationality_Id                        
    ,cnt2.[Country_Key]                     AS [Second_Country_Of_Nationality_Key]          -- Second_Country_Of_Nationality_Id             
    ,cnt3.[Country_Key]                     AS [Country_Of_Residency_Key]                   -- Country_Of_Residency_Id                          
    ,cnt4.[Country_Key]                     AS [Country_Lived_In_Key]                       -- Country_Lived_In_Id                              
    ,cnt5.[Country_Key]                     AS [Event_Country_Key]                          -- Event_Country_Id                             
    ,pt.[Programme_Type_Key]                                                                -- Programme_Type_Code                              
    ,p.[Programme_Key]                                                                      -- Programme_Code                                   
    ,camp1.[Campaign_Key]                   AS [Marketing_Campaign_Key]                     -- Marketing_Campaign_Id                            
    ,camp2.[Campaign_Key]                   AS [List_Load_Campaign_Key]                     -- List_Load_Campaign_Id                            
    ,camp3.[Campaign_Key]                   AS [Event_Campaign_Key]                         -- Event_Campaign_Id                                
    ,camp4.[Campaign_Key]                   AS [Trigger_Marketing_Campaign_Key]             -- Trigger_Marketing_Campaign_Id                    
    ,d2.[Date_Key]                          AS [Score_Removed_Date_Key]                     -- Score_Removed_Date       
    ,a.[Account_Key]                                                                        -- Account_Id       

-- Metrics
    ,ce.[Programme_Interest_Score]                                      
    ,ce.[Customer_Engagement_Score]                                 
    ,ce.[Customer_Engagement_Default_Score]                         
    ,ce.[Multi_Campaign_Attribution_Flag]                                                                   
    ,ce.[New_Contact_Flag]                                                                      
    ,ce.[Customer_Engagement_Count]                                                                 
    ,ce.[Entity_Record_Source]                                                                                          



    ,CONVERT(BIGINT, HASHBYTES('SHA1', CONCAT(
                                            [Customer_Engagement_Id] ,cec.[Customer_Engagement_Context_Key] ,d.[Date_Key] ,t.[TimeKey] ,c.[Contact_Key],age.[Age_Key],pic.[Programme_Interest_Context_Key] ,cnt1.[Country_Key] ,cnt2.[Country_Key] ,cnt3.[Country_Key] ,cnt4.[Country_Key] ,cnt5.[Country_Key] ,pt.[Programme_Type_Key] ,p.[Programme_Key] ,camp1.[Campaign_Key] ,camp2.[Campaign_Key] ,camp3.[Campaign_Key] ,camp4.[Campaign_Key] ,d2.[Date_Key] ,a.[Account_Key] ,ce.[Programme_Interest_Score] ,ce.[Customer_Engagement_Score] ,ce.[Customer_Engagement_Default_Score] ,ce.[Multi_Campaign_Attribution_Flag] ,ce.[New_Contact_Flag] ,ce.[Customer_Engagement_Count] ,ce.[Entity_Record_Source] --,[Age_On_Engagement_Key]

    ))) AS HashId


INTO   #CustEng_stg
FROM   [Integration].[Customer_Engagement] ce

LEFT OUTER JOIN [Reporting].[Dim_Contact] c
    ON ce.[LBS_Number] = c.[LBS_Number]
    AND CONVERT(date,CONVERT(varchar,[Engagement_Date])) BETWEEN c.[Row_Effective_Date] AND c.[Row_Expiry_Date]

LEFT OUTER JOIN [Reporting].[Dim_Customer_Engagement_Context] cec
    ON ce.[Engagement_Type_Id] = cec.[Engagement_Type_Id]

LEFT OUTER JOIN [Reporting].[Dim_Programme_Interest_Context] pic
    ON ce.[Programme_Interest_Status_Code] = pic.[Programme_Interest_Status_Code]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp1
    ON ce.[Marketing_Campaign_Id] = camp1.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp2
    ON ce.[List_Load_Campaign_Id] = camp2.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp3
    ON ce.[Event_Country_Id] = camp3.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp4
    ON ce.[Trigger_Marketing_Campaign_Id] = camp4.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Programme] p
    ON ce.[Programme_Code] = p.[Programme_Code]
    AND CONVERT(date,CONVERT(varchar,[Engagement_Date])) BETWEEN p.[Row_Effective_Date] AND p.[Row_Expiry_Date]

LEFT OUTER JOIN [Reporting].[Dim_Programme_Type] pt
    ON ce.[Programme_Type_Code] = pt.[Programme_Type_Code]

LEFT OUTER JOIN [Reporting].[Dim_Account] a
    ON ce.[Account_Id] = a.[Account_Id]
    AND CONVERT(date,CONVERT(varchar,[Engagement_Date])) BETWEEN a.[Row_Effective_Date] AND a.[Row_Expiry_Date]

LEFT OUTER JOIN [Reporting].[DimDate] d
    ON ce.[Engagement_Date] = d.[Date_Key]

LEFT OUTER JOIN [Reporting].[DimDate] d2
    ON ce.[Score_Removed_Date] = d.[Date_Key]

LEFT OUTER JOIN [Reporting].[DimTime] t
    ON ce.[Engagement_Time] = t.[TimeKey]

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt1                                      
    ON ce.[Country_Of_Nationality_Id] = cnt1.[Country_Id]                                   

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt2                                              
    ON ce.[Second_Country_Of_Nationality_Id] = cnt2.[Country_Id]                                                    

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt3
    ON ce.[Country_Of_Residency_Id] = cnt3.[Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt4
    ON ce.[Country_Lived_In_Id] = cnt4.[Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt5
    ON ce.[Event_Country_Id] = cnt5.[Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Age] age
    ON ce.[Age_On_Engagement] = age.[Age]

SELECT *
INTO   #CustEng_delta
FROM   #CustEng_stg AS s
WHERE  NOT EXISTS (SELECT 1
                    FROM   [Reporting].[Fact_Customer_Engagement] AS a
                    WHERE  a.HashId = s.HashId);

IF OBJECT_ID('tempdb..#CustEng_stg') IS NOT NULL
    DROP TABLE #CustEng_stg;

MERGE INTO Reporting.Fact_Customer_Engagement AS T

USING #CustEng_delta AS S ON (S.[Customer_Engagement_Id] = T.[Customer_Engagement_Id])

WHEN MATCHED THEN UPDATE 

SET 

        T.[Customer_Engagement_Context_Key]             =   S.[Customer_Engagement_Context_Key]                     
    ,T.[Engagement_Date_Key]                            =   S.[Engagement_Date_Key]             
    ,T.[Engagement_Time_Key]                            =   S.[Engagement_Time_Key]             
    ,T.[Contact_Key]                                    =   S.[Contact_Key]                     
    ,T.[Age_On_Engagement_Key]                          =   S.[Age_On_Engagement_Key]           
    ,T.[Programme_Interest_Context_Key]                 =   S.[Programme_Interest_Context_Key]      
    ,T.[Country_Of_Nationality_Key]                     =   S.[Country_Of_Nationality_Key]          
    ,T.[Second_Country_Of_Nationality_Key]              =   S.[Second_Country_Of_Nationality_Key]   
    ,T.[Country_Of_Residency_Key]                       =   S.[Country_Of_Residency_Key]            
    ,T.[Country_Lived_In_Key]                           =   S.[Country_Lived_In_Key]                
    ,T.[Event_Country_Key]                              =   S.[Event_Country_Key]                   
    ,T.[Programme_Type_Key]                             =   S.[Programme_Type_Key]                  
    ,T.[Programme_Key]                                  =   S.[Programme_Key]                       
    ,T.[Marketing_Campaign_Key]                         =   S.[Marketing_Campaign_Key]              
    ,T.[List_Load_Campaign_Key]                         =   S.[List_Load_Campaign_Key]              
    ,T.[Event_Campaign_Key]                             =   S.[Event_Campaign_Key]                  
    ,T.[Trigger_Marketing_Campaign_Key]                 =   S.[Trigger_Marketing_Campaign_Key]      
    ,T.[Score_Removed_Date_Key]                         =   S.[Score_Removed_Date_Key]              
    ,T.[Account_Key]                                    =   S.[Account_Key]                     
    ,T.[Programme_Interest_Score]                       =   S.[Programme_Interest_Score]            
    ,T.[Customer_Engagement_Score]                      =   S.[Customer_Engagement_Score]           
    ,T.[Customer_Engagement_Default_Score]              =   S.[Customer_Engagement_Default_Score]   
    ,T.[Multi_Campaign_Attribution_Flag]                =   S.[Multi_Campaign_Attribution_Flag] 
    ,T.[New_Contact_Flag]                               =   S.[New_Contact_Flag]                    
    ,T.[Customer_Engagement_Count]                      =   S.[Customer_Engagement_Count]
    ,T.[HashId]                                         =   S.[HashId]
    ,T.[Last_Updated_Date_Time]                         =   GETDATE()           
    ,T.[Entity_Record_Source]                           =   S.[Entity_Record_Source]                




WHEN NOT MATCHED THEN 


INSERT ([Customer_Engagement_Id], [Customer_Engagement_Context_Key], [Engagement_Date_Key], [Engagement_Time_Key], [Contact_Key], [Age_On_Engagement_Key], [Programme_Interest_Context_Key], [Country_Of_Nationality_Key], [Second_Country_Of_Nationality_Key], [Country_Of_Residency_Key], [Country_Lived_In_Key], [Event_Country_Key], [Programme_Type_Key], [Programme_Key], [Marketing_Campaign_Key], [List_Load_Campaign_Key], [Event_Campaign_Key], [Trigger_Marketing_Campaign_Key], [Score_Removed_Date_Key], [Account_Key], [Programme_Interest_Score], [Customer_Engagement_Score], [Customer_Engagement_Default_Score], [Multi_Campaign_Attribution_Flag], [New_Contact_Flag], [Customer_Engagement_Count], [HashId], [Last_Updated_Date_Time], [Entity_Record_Source])

VALUES

        (    S.[Customer_Engagement_Id]
            ,S.[Customer_Engagement_Context_Key]        
            ,S.[Engagement_Date_Key]                
            ,S.[Engagement_Time_Key]                
            ,S.[Contact_Key]                        
            ,S.[Age_On_Engagement_Key]                                              
            ,S.[Programme_Interest_Context_Key]     
            ,S.[Country_Of_Nationality_Key]         
            ,S.[Second_Country_Of_Nationality_Key]  
            ,S.[Country_Of_Residency_Key]           
            ,S.[Country_Lived_In_Key]               
            ,S.[Event_Country_Key]                  
            ,S.[Programme_Type_Key]                 
            ,S.[Programme_Key]                      
            ,S.[Marketing_Campaign_Key]             
            ,S.[List_Load_Campaign_Key]             
            ,S.[Event_Campaign_Key]                 
            ,S.[Trigger_Marketing_Campaign_Key]     
            ,S.[Score_Removed_Date_Key]             
            ,S.[Account_Key]                        
            ,S.[Programme_Interest_Score]           
            ,S.[Customer_Engagement_Score]          
            ,S.[Customer_Engagement_Default_Score]  
            ,S.[Multi_Campaign_Attribution_Flag]    
            ,S.[New_Contact_Flag]                   
            ,S.[Customer_Engagement_Count]
            ,S.[HashId]
            ,GETDATE()          
            ,S.[Entity_Record_Source]
        )
        ;
IF OBJECT_ID('tempdb..#CustEng_stg') IS NOT NULL
    DROP TABLE #CustEng_stg;
IF OBJECT_ID('tempdb..#CustEng_delta') IS NOT NULL
    DROP TABLE #CustEng_delta;  
问题出在第一个INSERT INTO语句中。预计执行计划如下:

/****** Object:  StoredProcedure [Reporting].[Merge_Fact_Customer_Engagement]    Script Date: 23/08/2019 14:32:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER   PROCEDURE [Reporting].[Merge_Fact_Customer_Engagement]
AS


IF OBJECT_ID('tempdb..#CustEng_stg') IS NOT NULL
    DROP TABLE #CustEng_stg;
IF OBJECT_ID('tempdb..#CustEng_delta') IS NOT NULL
    DROP TABLE #CustEng_delta;                                                                                      


SELECT

-- Business Key                                                                                                 
    [Customer_Engagement_Id]        

-- Foreign Keys                                         
    ,cec.[Customer_Engagement_Context_Key]                                                  -- Engagement_Type_Id                               
    ,d.[Date_Key]                           AS [Engagement_Date_Key]                        -- Engagement_Date                                  
    ,t.[TimeKey]                            AS [Engagement_Time_Key]                        -- Engagement_Time                                  
    ,c.[Contact_Key]                                                                        -- LBS_Number                                       
    ,age.[Age_Key]                          AS [Age_On_Engagement_Key]                      -- Age_On_Engagement                                
    ,pic.[Programme_Interest_Context_Key]                                                   -- Programme_Interest_Status_Code                   
    ,cnt1.[Country_Key]                     AS [Country_Of_Nationality_Key]                 -- Country_Of_Nationality_Id                        
    ,cnt2.[Country_Key]                     AS [Second_Country_Of_Nationality_Key]          -- Second_Country_Of_Nationality_Id             
    ,cnt3.[Country_Key]                     AS [Country_Of_Residency_Key]                   -- Country_Of_Residency_Id                          
    ,cnt4.[Country_Key]                     AS [Country_Lived_In_Key]                       -- Country_Lived_In_Id                              
    ,cnt5.[Country_Key]                     AS [Event_Country_Key]                          -- Event_Country_Id                             
    ,pt.[Programme_Type_Key]                                                                -- Programme_Type_Code                              
    ,p.[Programme_Key]                                                                      -- Programme_Code                                   
    ,camp1.[Campaign_Key]                   AS [Marketing_Campaign_Key]                     -- Marketing_Campaign_Id                            
    ,camp2.[Campaign_Key]                   AS [List_Load_Campaign_Key]                     -- List_Load_Campaign_Id                            
    ,camp3.[Campaign_Key]                   AS [Event_Campaign_Key]                         -- Event_Campaign_Id                                
    ,camp4.[Campaign_Key]                   AS [Trigger_Marketing_Campaign_Key]             -- Trigger_Marketing_Campaign_Id                    
    ,d2.[Date_Key]                          AS [Score_Removed_Date_Key]                     -- Score_Removed_Date       
    ,a.[Account_Key]                                                                        -- Account_Id       

-- Metrics
    ,ce.[Programme_Interest_Score]                                      
    ,ce.[Customer_Engagement_Score]                                 
    ,ce.[Customer_Engagement_Default_Score]                         
    ,ce.[Multi_Campaign_Attribution_Flag]                                                                   
    ,ce.[New_Contact_Flag]                                                                      
    ,ce.[Customer_Engagement_Count]                                                                 
    ,ce.[Entity_Record_Source]                                                                                          



    ,CONVERT(BIGINT, HASHBYTES('SHA1', CONCAT(
                                            [Customer_Engagement_Id] ,cec.[Customer_Engagement_Context_Key] ,d.[Date_Key] ,t.[TimeKey] ,c.[Contact_Key],age.[Age_Key],pic.[Programme_Interest_Context_Key] ,cnt1.[Country_Key] ,cnt2.[Country_Key] ,cnt3.[Country_Key] ,cnt4.[Country_Key] ,cnt5.[Country_Key] ,pt.[Programme_Type_Key] ,p.[Programme_Key] ,camp1.[Campaign_Key] ,camp2.[Campaign_Key] ,camp3.[Campaign_Key] ,camp4.[Campaign_Key] ,d2.[Date_Key] ,a.[Account_Key] ,ce.[Programme_Interest_Score] ,ce.[Customer_Engagement_Score] ,ce.[Customer_Engagement_Default_Score] ,ce.[Multi_Campaign_Attribution_Flag] ,ce.[New_Contact_Flag] ,ce.[Customer_Engagement_Count] ,ce.[Entity_Record_Source] --,[Age_On_Engagement_Key]

    ))) AS HashId


INTO   #CustEng_stg
FROM   [Integration].[Customer_Engagement] ce

LEFT OUTER JOIN [Reporting].[Dim_Contact] c
    ON ce.[LBS_Number] = c.[LBS_Number]
    AND CONVERT(date,CONVERT(varchar,[Engagement_Date])) BETWEEN c.[Row_Effective_Date] AND c.[Row_Expiry_Date]

LEFT OUTER JOIN [Reporting].[Dim_Customer_Engagement_Context] cec
    ON ce.[Engagement_Type_Id] = cec.[Engagement_Type_Id]

LEFT OUTER JOIN [Reporting].[Dim_Programme_Interest_Context] pic
    ON ce.[Programme_Interest_Status_Code] = pic.[Programme_Interest_Status_Code]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp1
    ON ce.[Marketing_Campaign_Id] = camp1.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp2
    ON ce.[List_Load_Campaign_Id] = camp2.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp3
    ON ce.[Event_Country_Id] = camp3.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Campaign] camp4
    ON ce.[Trigger_Marketing_Campaign_Id] = camp4.[Campaign_Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Programme] p
    ON ce.[Programme_Code] = p.[Programme_Code]
    AND CONVERT(date,CONVERT(varchar,[Engagement_Date])) BETWEEN p.[Row_Effective_Date] AND p.[Row_Expiry_Date]

LEFT OUTER JOIN [Reporting].[Dim_Programme_Type] pt
    ON ce.[Programme_Type_Code] = pt.[Programme_Type_Code]

LEFT OUTER JOIN [Reporting].[Dim_Account] a
    ON ce.[Account_Id] = a.[Account_Id]
    AND CONVERT(date,CONVERT(varchar,[Engagement_Date])) BETWEEN a.[Row_Effective_Date] AND a.[Row_Expiry_Date]

LEFT OUTER JOIN [Reporting].[DimDate] d
    ON ce.[Engagement_Date] = d.[Date_Key]

LEFT OUTER JOIN [Reporting].[DimDate] d2
    ON ce.[Score_Removed_Date] = d.[Date_Key]

LEFT OUTER JOIN [Reporting].[DimTime] t
    ON ce.[Engagement_Time] = t.[TimeKey]

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt1                                      
    ON ce.[Country_Of_Nationality_Id] = cnt1.[Country_Id]                                   

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt2                                              
    ON ce.[Second_Country_Of_Nationality_Id] = cnt2.[Country_Id]                                                    

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt3
    ON ce.[Country_Of_Residency_Id] = cnt3.[Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt4
    ON ce.[Country_Lived_In_Id] = cnt4.[Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Country] cnt5
    ON ce.[Event_Country_Id] = cnt5.[Country_Id]

LEFT OUTER JOIN [Reporting].[Dim_Age] age
    ON ce.[Age_On_Engagement] = age.[Age]

SELECT *
INTO   #CustEng_delta
FROM   #CustEng_stg AS s
WHERE  NOT EXISTS (SELECT 1
                    FROM   [Reporting].[Fact_Customer_Engagement] AS a
                    WHERE  a.HashId = s.HashId);

IF OBJECT_ID('tempdb..#CustEng_stg') IS NOT NULL
    DROP TABLE #CustEng_stg;

MERGE INTO Reporting.Fact_Customer_Engagement AS T

USING #CustEng_delta AS S ON (S.[Customer_Engagement_Id] = T.[Customer_Engagement_Id])

WHEN MATCHED THEN UPDATE 

SET 

        T.[Customer_Engagement_Context_Key]             =   S.[Customer_Engagement_Context_Key]                     
    ,T.[Engagement_Date_Key]                            =   S.[Engagement_Date_Key]             
    ,T.[Engagement_Time_Key]                            =   S.[Engagement_Time_Key]             
    ,T.[Contact_Key]                                    =   S.[Contact_Key]                     
    ,T.[Age_On_Engagement_Key]                          =   S.[Age_On_Engagement_Key]           
    ,T.[Programme_Interest_Context_Key]                 =   S.[Programme_Interest_Context_Key]      
    ,T.[Country_Of_Nationality_Key]                     =   S.[Country_Of_Nationality_Key]          
    ,T.[Second_Country_Of_Nationality_Key]              =   S.[Second_Country_Of_Nationality_Key]   
    ,T.[Country_Of_Residency_Key]                       =   S.[Country_Of_Residency_Key]            
    ,T.[Country_Lived_In_Key]                           =   S.[Country_Lived_In_Key]                
    ,T.[Event_Country_Key]                              =   S.[Event_Country_Key]                   
    ,T.[Programme_Type_Key]                             =   S.[Programme_Type_Key]                  
    ,T.[Programme_Key]                                  =   S.[Programme_Key]                       
    ,T.[Marketing_Campaign_Key]                         =   S.[Marketing_Campaign_Key]              
    ,T.[List_Load_Campaign_Key]                         =   S.[List_Load_Campaign_Key]              
    ,T.[Event_Campaign_Key]                             =   S.[Event_Campaign_Key]                  
    ,T.[Trigger_Marketing_Campaign_Key]                 =   S.[Trigger_Marketing_Campaign_Key]      
    ,T.[Score_Removed_Date_Key]                         =   S.[Score_Removed_Date_Key]              
    ,T.[Account_Key]                                    =   S.[Account_Key]                     
    ,T.[Programme_Interest_Score]                       =   S.[Programme_Interest_Score]            
    ,T.[Customer_Engagement_Score]                      =   S.[Customer_Engagement_Score]           
    ,T.[Customer_Engagement_Default_Score]              =   S.[Customer_Engagement_Default_Score]   
    ,T.[Multi_Campaign_Attribution_Flag]                =   S.[Multi_Campaign_Attribution_Flag] 
    ,T.[New_Contact_Flag]                               =   S.[New_Contact_Flag]                    
    ,T.[Customer_Engagement_Count]                      =   S.[Customer_Engagement_Count]
    ,T.[HashId]                                         =   S.[HashId]
    ,T.[Last_Updated_Date_Time]                         =   GETDATE()           
    ,T.[Entity_Record_Source]                           =   S.[Entity_Record_Source]                




WHEN NOT MATCHED THEN 


INSERT ([Customer_Engagement_Id], [Customer_Engagement_Context_Key], [Engagement_Date_Key], [Engagement_Time_Key], [Contact_Key], [Age_On_Engagement_Key], [Programme_Interest_Context_Key], [Country_Of_Nationality_Key], [Second_Country_Of_Nationality_Key], [Country_Of_Residency_Key], [Country_Lived_In_Key], [Event_Country_Key], [Programme_Type_Key], [Programme_Key], [Marketing_Campaign_Key], [List_Load_Campaign_Key], [Event_Campaign_Key], [Trigger_Marketing_Campaign_Key], [Score_Removed_Date_Key], [Account_Key], [Programme_Interest_Score], [Customer_Engagement_Score], [Customer_Engagement_Default_Score], [Multi_Campaign_Attribution_Flag], [New_Contact_Flag], [Customer_Engagement_Count], [HashId], [Last_Updated_Date_Time], [Entity_Record_Source])

VALUES

        (    S.[Customer_Engagement_Id]
            ,S.[Customer_Engagement_Context_Key]        
            ,S.[Engagement_Date_Key]                
            ,S.[Engagement_Time_Key]                
            ,S.[Contact_Key]                        
            ,S.[Age_On_Engagement_Key]                                              
            ,S.[Programme_Interest_Context_Key]     
            ,S.[Country_Of_Nationality_Key]         
            ,S.[Second_Country_Of_Nationality_Key]  
            ,S.[Country_Of_Residency_Key]           
            ,S.[Country_Lived_In_Key]               
            ,S.[Event_Country_Key]                  
            ,S.[Programme_Type_Key]                 
            ,S.[Programme_Key]                      
            ,S.[Marketing_Campaign_Key]             
            ,S.[List_Load_Campaign_Key]             
            ,S.[Event_Campaign_Key]                 
            ,S.[Trigger_Marketing_Campaign_Key]     
            ,S.[Score_Removed_Date_Key]             
            ,S.[Account_Key]                        
            ,S.[Programme_Interest_Score]           
            ,S.[Customer_Engagement_Score]          
            ,S.[Customer_Engagement_Default_Score]  
            ,S.[Multi_Campaign_Attribution_Flag]    
            ,S.[New_Contact_Flag]                   
            ,S.[Customer_Engagement_Count]
            ,S.[HashId]
            ,GETDATE()          
            ,S.[Entity_Record_Source]
        )
        ;
IF OBJECT_ID('tempdb..#CustEng_stg') IS NOT NULL
    DROP TABLE #CustEng_stg;
IF OBJECT_ID('tempdb..#CustEng_delta') IS NOT NULL
    DROP TABLE #CustEng_delta;  

我不确定连接是否导致了问题

编辑:

问题是在连接中,有一些多对多的关系我没有处理好,所以我正在经历并找出罪犯在哪里。 谢谢你所有的答案,当我把它修好后再发一次

编辑2:

更新的查询计划-那些连接非常错误,但现在已经修复了!

祝贺Matt Lakin的问题现在得到了解决。感谢评论中的所有帮助

  • 这是一个多对多的问题
  • Matt Lakin现已修复,以查看更新的查询计划:

  • 我帮助Matt Lakin将此作为答案发布,并希望这对其他社区成员有益。

    我遇到的问题是连接条件,一些“ON”匹配创建了多对多连接,从而创建了大量重复行,如第一个执行计划中的估计行所示

    我通过对基表运行COUNT()查询,然后在随后添加联接时将结果与COUNT()进行比较,解决了这个问题,例如:

  • 计算基数
  • 计算包括第一次加入在内的数量
  • 比较(1)和(2)之间的计数。如果(2)中的计数较高,则连接存在问题

  • 继续进行其余的连接,注意计数增加的位置


  • 该select语句返回多少行?估计计划中插入的估计行数高得离谱。它无法完全执行,所以我不确定。我认为其中一个连接中肯定存在一些多对多问题。当前正在逐个包含联接时进行行计数。在多个联接条件上运行转换会造成很大的伤害,而在其上运行一个看似庞大的字段串联成散列?是的。平面图显示了估计的行数6.56525E+15估计的行大小9b。这是很多行和字节。超过32GB。估计的计划是6.525E+15行。你需要回到这里的开头,弄清楚你想做什么。还要找到一种过滤初始查询的方法,这样就不会将所有数据都插入临时表,然后再将其中的一些数据插入到将要使用的另一个临时表中。
       SELECT COUNT(*)
    
       FROM   [Integration].[Customer_Engagement] ce
    
       LEFT OUTER JOIN [Reporting].[Dim_Contact] c
       ON ce.[LBS_Number] = c.[LBS_Number]
       AND CONVERT(date,CONVERT(varchar,[Engagement_Date])) BETWEEN c.[Row_Effective_Date] 
       AND c.[Row_Expiry_Date]