SQL插入使用CTE

时间:2015-01-26 18:40:14

标签: tsql common-table-expression

由于sql中的“Insert into”语句,我遇到了性能问题。我正在使用CTE从多个表中选择数据并插入到其他表中。它工作得很好,直到昨天。选择只需不到一分钟的时间来检索插入的数据。有人可以帮助我理解我做错了什么。任何帮助都非常感谢。感谢。

这是我的代码:

我在SP中使用此查询。我正在尝试将220K记录加载到1.5M记录表。

;with CTE_A
AS
(
      SELECT A1, A2,...           
      FROM dbo.A  with (nolock)
      WHERE A1 = <some condition>  
      GROUP BY a.A1,a.A2 , a.A3    
), CTE_C as
   (
       SELECT C1, C2,....                
       FROM dbo.B with (nolock)       
       WHERE a.C1 = <some condition>   
       GROUP BY a.c1,a.C2 , a.C3      
)
INSERT INTO [dbo].MainTable
    SELECT 
        A1, A2, A3 , C1, C2, C3       
    FROM 
        CTE_A ta with (nolock)
    LEFT OUTER JOIN 
        CTE_C tc with (nolock) ON ta.a1 = tc.a1 and ta.b1 = tc.b1 and ta.c1 = tc.c1 
    LEFT OUTER JOIN 
        othertable bs with (nolock) ON usd_bs.c = s.c   
                                        AND (A1 BETWEEN bs.a1 AND bs.a1)
                                        AND bs.c1 = 1                     

1 个答案:

答案 0 :(得分:1)

尝试这种方法(临时表而不是cte),性能必须高得多,才能完成任务

IF OBJECT_ID('Tempdb..#CTE_A') IS NOT NULL 
    DROP TABLE #CTE_A
IF OBJECT_ID('Tempdb..#CTE_C') IS NOT NULL 
    DROP TABLE #CTE_C
-------------------------------------------------------------
SELECT  A1 ,
        A2 ,...   
INTO    #CTE_A --data set into temp table
FROM    dbo.A WITH ( NOLOCK )
WHERE A1 = <some condition>  
GROUP BY a.A1 ,
        a.A2 ,
        a.A3
-------------------------------------------------------------
SELECT  C1 ,
        C2 ,....                
FROM    dbo.B WITH ( NOLOCK )       
INTO  #CTE_C --data set into temp table
WHERE a.C1 = <some condition>   
GROUP BY a.c1 ,
        a.C2 ,
        a.C3
INSERT  INTO [dbo].MainTable
        SELECT  A1 ,
                A2 ,
                A3 ,
                C1 ,
                C2 ,
                C3
        FROM    #CTE_A AS ta
                LEFT JOIN #CTE_C AS tc ON ta.a1 = tc.a1
                                          AND ta.b1 = tc.b1
                                          AND ta.c1 = tc.c1
                LEFT JOIN othertable AS bs ON usd_bs.c = s.c
                                              AND ( A1 BETWEEN bs.a1 AND bs.a1 )
                                              AND bs.c1 = 1