The query is executing very slowly, is there any way to improve it any further?

后端 未结 8 1130
长情又很酷
长情又很酷 2021-02-15 17:26

I have the following query, and because of a lot of SUM function calls, my query is running too slow. I have a lot of records in my database and I would like to get

8条回答
  •  难免孤独
    2021-02-15 18:00

    The best approach is to insert into a table variable/hash table (if the row count is small use a table variable or use a hash table if the row count is pretty much big). Then update the aggregation and then finally select from the table variable or hash table. Looking into the query plan is necessary.

    DECLARE @MYTABLE TABLE (ID INT, [Title] VARCHAR(500), [Class] VARCHAR(500),
    [Current - Last 30 Days Col1] INT, [Current - Last 30 Days Col2] INT,
    [Current - Last 90 Days Col1] INT,[Current - Last 90 Days Col2] INT,
    [Current - Last 365 Days Col1] INT, [Current - Last 365 Days Col2] INT,
    [Last year - Last 30 Days Col1] INT, [Last year - Last 30 Days Col2] INT,
    [Last year - Last 90 Days Col1] INT, [Last year - Last 90 Days Col2] INT,
    [Last year - Last 365 Days Col1] INT, [Last year - Last 365 Days Col2] INT)
    
    
    
    INSERT INTO @MYTABLE(ID, [Title],[Class], 
    [Current - Last 30 Days Col1], [Current - Last 30 Days Col2],
    [Current - Last 90 Days Col1], [Current - Last 90 Days Col2],
    [Current - Last 365 Days Col1], [Current - Last 365 Days Col2],
    [Last year - Last 30 Days Col1], [Last year - Last 30 Days Col2],
    [Last year - Last 90 Days Col1], [Last year - Last 90 Days Col2],
    [Last year - Last 365 Days Col1], [Last year - Last 365 Days Col2]
      )
    SELECT    b.id  ,d.[Title] ,e.Class ,0,0,0,0,0,0,0,0,0,0,0,0        
    FROM     tb1 a
    INNER JOIN   tb2 b on a.id=b.fid and a.col3 = b.col4
    INNER JOIN   tb3 c on b.fid = c.col5
    INNER JOIN   tb4 d on c.id = d.col6
    INNER JOIN  tb5 e on c.col7 = e.id
    GROUP BY b.id, d.Title, e.Class
    
    UPDATE T 
    SET [Current - Last 30 Days Col1]=K.[Current - Last 30 Days Col1] , 
    [Current - Last 30 Days Col2]    =K.[Current - Last 30 Days Col2],
    [Current - Last 90 Days Col1]    = K.[Current - Last 90 Days Col1], 
    [Current - Last 90 Days Col2]    =K.[Current - Last 90 Days Col2] ,
    [Current - Last 365 Days Col1]   =K.[Current - Last 365 Days Col1], 
    [Current - Last 365 Days Col2]   =K.[Current - Last 365 Days Col2],
    [Last year - Last 30 Days Col1]  =K.[Last year - Last 30 Days Col1],
     [Last year - Last 30 Days Col2] =K.[Last year - Last 30 Days Col2],
    [Last year - Last 90 Days Col1]  =K.[Last year - Last 90 Days Col1], 
    [Last year - Last 90 Days Col2]  =K.[Last year - Last 90 Days Col2],
    [Last year - Last 365 Days Col1] =K.[Last year - Last 365 Days Col1],
     [Last year - Last 365 Days Col2]=K.[Last year - Last 365 Days Col2]
        FROM @MYTABLE T JOIN 
         (
    SELECT 
        b.id as [ID]
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(MONTH,-1,GETDATE()) THEN a.col1 ELSE 0 END),0) as [Current - Last 30 Days Col1]
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(MONTH,-1,GETDATE()) THEN a.col2 ELSE 0 END),0) as [Current - Last 30 Days Col2]
    
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(QUARTER,-1,GETDATE()) THEN a.col1 ELSE 0 END),0) as [Current - Last 90 Days Col1]
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(QUARTER,-1,GETDATE()) THEN a.col2 ELSE 0 END),0) as [Current - Last 90 Days Col2]
    
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(YEAR,-1,GETDATE()) THEN a.col1 ELSE 0 END),0) as [Current - Last 365 Days Col1]
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(YEAR,-1,GETDATE()) THEN a.col2 ELSE 0 END),0) as [Current - Last 365 Days Col2]
    
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(MONTH,-13,GETDATE()) and a.DateCol <= DATEADD(MONTH,-12,GETDATE()) THEN a.col1 ELSE 0 END),0) as [Last year - Last 30 Days Col1]
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(MONTH,-13,GETDATE()) and a.DateCol <= DATEADD(MONTH,-12,GETDATE()) THEN a.col2 ELSE 0 END),0) as [Last year - Last 30 Days Col2]
    
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(QUARTER,-5,GETDATE()) and a.DateCol <= DATEADD(QUARTER,-4,GETDATE()) THEN a.col1 ELSE 0 END),0) as [Last year - Last 90 Days Col1]
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(QUARTER,-5,GETDATE()) and a.DateCol <= DATEADD(QUARTER,-4,GETDATE()) THEN a.col2 ELSE 0 END),0) as [Last year - Last 90 Days Col2]
    
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(YEAR,-2,GETDATE()) and a.DateCol <= DATEADD(YEAR,-1,GETDATE()) THEN a.col1 ELSE 0 END),0) as [Last year - Last 365 Days Col1]
        ,ISNULL(Sum(CASE WHEN a.DateCol >= DATEADD(YEAR,-2,GETDATE()) and a.DateCol <= DATEADD(YEAR,-1,GETDATE()) THEN a.col2 ELSE 0 END),0) as [Last year - Last 365 Days Col2]
        FROM     tb1 a
    INNER JOIN   tb2 b on a.id=b.fid and a.col3 = b.col4
    INNER JOIN   tb3 c on b.fid = c.col5
    INNER JOIN   tb4 d on c.id = d.col6
    INNER JOIN  tb5 e on c.col7 = e.id
    GROUP BY    b.id
    ) AS K ON T.ID=K.ID
    
    
    SELECT *
    FROM @MYTABLE
    

提交回复
热议问题