我如何优化SQL查询

时间:2018-09-24 07:26:45

标签: sql-server

我想在下面的SQL查询中进行优化,这需要很长时间才能获得所需的输出,我只是在这里发布需要花费很长时间才能处理的特定查询。

当前在表中没有任何索引,请建议如何优化此查询以使其表现更好,也请告知我是否要为表创建任何索引

Insert into tbl_Details
    SELECT a.[COST],a.[Ch_Group] as 'CHGROUP',a.[PRODHIR],d.[MATERIAL],
    a.[UCC],
        CASE WHEN SUM(CAST(d.[Qty3] AS FLOAT))<='0.00000' THEN '0.00000'
        ELSE
         (
        SUM(CAST(d.[Qty3] AS FLOAT))/
        (SELECT (SUM(CAST(p.[Qty3] AS FLOAT))) AS 'SUM'
        FROM [tbl_OP6] p WHERE CAST(p.[Qty3] as FLOAT)>0 AND p.[PROD_HIER] LIKE a.[PRODHIR]+'%' 
        AND CONCAT(p.[Qty2],p.[Qty3A],p.[Qty4A]) LIKE case when a.[Ch_Group] like 'PM60%' then '%' else a.[Ch_Group]+'%' end and p.[zsdistrib] like a.[UCC]+'%' 
        AND  p.[PROD_HIER]LIKE a.[PRODHIR]+'%'  AND CAST(p.[Qty3] AS FLOAT)>'0.00000')
        )
        end
             AS 'GSV'
        ,'' as 'BUFCG'  
        FROM TEMP2_New a
        INNER JOIN [tbl_OP6] d ON CONCAT(d.[Qty2],d.[Qty3A],d.[Qty4A]) LIKE case when a.[Ch_Group] like 'PM60%' then '%' else a.[Ch_Group]+'%' end and d.[zsdistrib] like a.[UCC]+'%'
        AND  d.[PROD_HIER]LIKE a.[PRODHIR]+'%'  AND CAST(d.[Qty3] AS FLOAT)>'0.00000'
        WHERE a.[RULE]='4'
        GROUP BY a.[COST],a.[Ch_Group],a.[PRODHIR],d.[MATERIAL],a.[ORDERTYPE],
        a.[VALUEFIELD],a.[UCC],
        CONCAT(d.[Qty2],d.[Qty3A],d.[Qty4A]),a.[COORDER],a.[PLANT],a.[SEND_CCTR]

0 个答案:

没有答案