在选择查询时,使用列名称非常慢

时间:2016-08-31 07:40:15

标签: sql sql-server sql-server-2012

我在sql server 2012中有这个查询

select *  
from (select a.AccountNo, a.AuxiliaryLatinDesc AccountDesc,
        a.AccountNo + ' '+ a.AuxiliaryLatinDesc AccountTitle, 
        SUM(h.Debit) Debit, SUM(h.Credit) Credit,
        SUM(h.Debit-h.Credit) Remain   
      FROM TblAuxiliary a 
          JOIN ViwDocHeaderItem h ON a.AccountNo = h.AccountNo 
      WHERE  h.DocDate>='01/01/2016'
         AND h.DocDate<='08/30/2016' 
         AND SUBSTRING(h.AccountNo,1,2)='11'
      GROUP BY a.AccountNo, a.AuxiliaryLatinDesc) t1   
    Full join  
     (SELECT a.AccountNo, a.AuxiliaryLatinDesc AccountDesc, 
          a.AccountNo + ' '+ a.AuxiliaryLatinDesc AccountTitle, 
      SUM(h.Debit) Debit, SUM(h.Credit) Credit, 
              SUM(h.Debit-h.Credit) opening 
      FROM TblAuxiliary a JOIN ViwDocHeaderItem h 
         ON a.AccountNo = h.AccountNo  
      WHERE h.DocDate between '01/01/2000' AND '01/01/2016'    
          AND SUBSTRING(h.AccountNo,1,2)='11' 
      GROUP BY   a.AccountNo, a.AuxiliaryLatinDesc) t2 
on  t1.AccountNo=t2.AccountNo 
ORDER BY t1.AccountNo 

非常快。但当我用这样的列名替换*时:

select t2.AccountNo  from (sELECT     TblAuxiliary.AccountNo     , TblAuxiliary.AuxiliaryLatinDesc AS AccountDesc,TblAuxiliary.AccountNo + ' '+TblAuxiliary.AuxiliaryLatinDesc AS AccountTitle
     , SUM(ViwDocHeaderItem.Debit) AS Debit, SUM(ViwDocHeaderItem.Credit) AS Credit,SUM(ViwDocHeaderItem.Debit-ViwDocHeaderItem.Credit) AS Remain     
     FROM TblAuxiliary INNER JOIN ViwDocHeaderItem ON TblAuxiliary.AccountNo = ViwDocHeaderItem.AccountNo WHERE  ViwDocHeaderItem.DocDate>='01/01/2016' AND ViwDocHeaderItem.DocDate<='08/30/2016' AND SUBSTRING(ViwDocHeaderItem.AccountNo,1,2)='11'      
      GROUP BY TblAuxiliary.AccountNo, TblAuxiliary.AuxiliaryLatinDesc  ) t1   Full outer join  
        (SELECT TblAuxiliary.AccountNo, TblAuxiliary.AuxiliaryLatinDesc AS AccountDesc,TblAuxiliary.AccountNo + ' '+TblAuxiliary.AuxiliaryLatinDesc AS AccountTitle, 
      SUM(ViwDocHeaderItem.Debit) AS Debit, SUM(ViwDocHeaderItem.Credit) AS Credit,SUM(ViwDocHeaderItem.Debit-ViwDocHeaderItem.Credit) AS opening 
       FROM TblAuxiliary INNER JOIN ViwDocHeaderItem ON TblAuxiliary.AccountNo = ViwDocHeaderItem.AccountNo  WHERE ViwDocHeaderItem.DocDate>='01/01/2000'  AND ViwDocHeaderItem.DocDate<'01/01/2016'    AND SUBSTRING(ViwDocHeaderItem.AccountNo,1,2)='11' 
        GROUP BY   TblAuxiliary.AccountNo, TblAuxiliary.AuxiliaryLatinDesc) t2 on  t1.AccountNo=t2.AccountNo ORDER BY t1.AccountNo 
它会非常慢。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下查询。

;WITH cte_1
 AS
    (SELECT  TblAuxiliary.AccountNo , TblAuxiliary.AuxiliaryLatinDesc AS AccountDesc,TblAuxiliary.AccountNo + ' '+TblAuxiliary.AuxiliaryLatinDesc AS AccountTitle
             ,SUM(ViwDocHeaderItem.Debit) AS Debit, SUM(ViwDocHeaderItem.Credit) AS Credit,SUM(ViwDocHeaderItem.Debit-ViwDocHeaderItem.Credit) AS Remain     
     FROM TblAuxiliary 
       INNER JOIN ViwDocHeaderItem ON TblAuxiliary.AccountNo = ViwDocHeaderItem.AccountNo 
     WHERE  ViwDocHeaderItem.DocDate>='01/01/2016' AND ViwDocHeaderItem.DocDate<='08/30/2016' AND SUBSTRING(ViwDocHeaderItem.AccountNo,1,2)='11'      
      GROUP BY TblAuxiliary.AccountNo, TblAuxiliary.AuxiliaryLatinDesc  ) 
,cte_2
  AS (SELECT TblAuxiliary.AccountNo, TblAuxiliary.AuxiliaryLatinDesc AS AccountDesc,TblAuxiliary.AccountNo + ' '+TblAuxiliary.AuxiliaryLatinDesc AS AccountTitle 
             ,SUM(ViwDocHeaderItem.Debit) AS Debit, SUM(ViwDocHeaderItem.Credit) AS Credit,SUM(ViwDocHeaderItem.Debit-ViwDocHeaderItem.Credit) AS opening 
      FROM TblAuxiliary INNER JOIN ViwDocHeaderItem ON TblAuxiliary.AccountNo = ViwDocHeaderItem.AccountNo  WHERE ViwDocHeaderItem.DocDate>='01/01/2000'  AND ViwDocHeaderItem.DocDate<'01/01/2016'    AND SUBSTRING(ViwDocHeaderItem.AccountNo,1,2)='11' 
      GROUP BY   TblAuxiliary.AccountNo, TblAuxiliary.AuxiliaryLatinDesc) 
SELECT t2.AccountNo
FROM  cte_1 t1
       FULL OUTER JOIN cte_2 t2  ON  t1.AccountNo=t2.AccountNo 
ORDER BY t1.AccountNo