在组中使用别名

时间:2015-02-05 03:30:35

标签: sql sql-server-2008 join group-by alias

我知道您不能在组中使用中的别名,因为它是在SELECT语句之前处理的。但是,如果我将原始SELECT语句嵌套在子查询中,是否有任何类型的性能损失?

它更容易阅读,至少对我而言,但我更关心表现。

我的原始查询是:

SELECT 
    Rec.Description [CO_Desc], 
    EM.Description [EM_Desc], 
    ISNULL(EM.UnitCost, '0') * DPaCT.ProcedureProjection [Proj_Cost],     
    ISNULL(EM.UnitCost, '0') * DPaCt.ProceduresPerformed [Act_Cost]
FROM 
    rpt_DriveMaster DM
LEFT OUTER JOIN
    rpt_Accounts Acct ON DM.AccountID = Acct.AccountID
LEFT OUTER JOIN 
    IDViewRecruiters Rec ON Acct.RecruiterID = Rec.CodeID
INNER JOIN 
    rpt_CenterDetail CD ON DM.CenterID = CD.CenterID
INNER JOIN 
    IDViewCollectionOp CO ON CD.CenterID = CO.CodeID
INNER JOIN 
    IDViewRegion Reg ON CD.Region = Reg.CodeID
INNER JOIN 
    IDViewOrgCenter Org ON CD.OrgCenter = Org.CodeID
INNER JOIN 
    IDViewOrgSubCenter Sub ON CD.OrgSubCenter = Sub.CodeID
INNER JOIN 
    rpt_DriveStatusDef DrStat ON DM.StatusID = DrStat.StatusID
INNER JOIN 
    DriveProjectionandCollectedTotals DPaCT ON DM.DriveID = DPaCT.DriveID
LEFT OUTER JOIN 
    EquipmentDetail ED ON DM.DriveID = ED.DriveID
LEFT OUTER JOIN 
    EquipmentMaster EM ON ED.EquipmentId = EM.EquipmentID
WHERE 
    DM.StatusID <> 5 AND 
    DM.FromDateTime BETWEEN '02/01/2014' AND '02/28/2014' AND
    CO.CodeID IN (106,107,108,110)
GROUP BY 
    Rec.Description, 
    EM.Description, 
    ISNULL(EM.UnitCost, '0') * DPaCT.ProcedureProjection,  
    ISNULL(EM.UnitCost, '0') * DPaCt.ProceduresPerformed

但我不喜欢GROUP BY读取的方式,所以我将查询更改为:

SELECT 
    A.Rec_Desc, 
    A.EM_Desc, 
    A.Proj_Cost, 
    A.Act_Cost
FROM
(
    SELECT
        Rec.Description [Rec_Desc], 
        EM.Description [EM_Desc],
        ISNULL(EM.UnitCost, '0') * DPaCT.ProcedureProjection [Proj_Cost], 
        ISNULL(EM.UnitCost, '0')  *DPaCt.ProceduresPerformed [Act_Cost]
    FROM 
        rpt_DriveMaster DM
    LEFT OUTER JOIN 
        rpt_Accounts Acct ON DM.AccountID = Acct.AccountID
    LEFT OUTER JOIN 
        IDViewRecruiters Rec ON Acct.RecruiterID = Rec.CodeID
    INNER JOIN 
        rpt_CenterDetail CD ON DM.CenterID = CD.CenterID
    INNER JOIN 
        IDViewCollectionOp CO ON CD.CenterID = CO.CodeID
    INNER JOIN 
        IDViewRegion Reg ON CD.Region = Reg.CodeID
    INNER JOIN 
        IDViewOrgCenter Org ON CD.OrgCenter = Org.CodeID
    INNER JOIN 
        IDViewOrgSubCenter Sub ON CD.OrgSubCenter = Sub.CodeID
    INNER JOIN 
        rpt_DriveStatusDef DrStat ON DM.StatusID = DrStat.StatusID
    INNER JOIN 
        DriveProjectionandCollectedTotals DPaCT ON DM.DriveID = DPaCT.DriveID
    LEFT OUTER JOIN 
        EquipmentDetail ED ON DM.DriveID = ED.DriveID
    LEFT OUTER JOIN 
        EquipmentMaster EM ON ED.EquipmentId = EM.EquipmentID
    WHERE
        DM.StatusID <> 5 AND 
        DM.FromDateTime BETWEEN '02/01/2014' AND '02/28/2014' AND
        CO.CodeID IN (106,107,108,110)) AS A
    GROUP BY 
        A.Rec_Desc, 
        A.EM_Desc, 
        A.Proj_Cost, 
        A.Act_Cost

我是否在查看查询性能的任何真正差异?

0 个答案:

没有答案