如何降低通过链接服务器执行的查询的成本

Posted

技术标签:

【中文标题】如何降低通过链接服务器执行的查询的成本【英文标题】:How to reduce cost of the query that is executed through a Linked Server 【发布时间】:2016-03-18 14:08:52 【问题描述】:

请看我的执行计划(在SQL10.NG上执行):

    Use [Reporting]

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

   ALTER PROCEDURE [dbo].[sp_WarehouseAttendance_New]

    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        ---------delete existing warehouse table------------------------------
    IF OBJECT_ID('dbo.WarehouseAttendance_New', 'U') IS NOT NULL
    TRUNCATE TABLE dbo.WarehouseAttendance_New; 
    --------Create New Table--------------------------------------------------
    ---Weeks--------------------------------------------------------------------
    With weeks as 
    (select VDC_week_no, VDC_cal_year, min(VDC_day_date) as WeekCommence
    from sql10.ng.dbo.Vdaily_calender
    where VDC_avail = 'T'
    group by VDC_week_no, VDC_cal_year),

    ---TTWeeks------------------------------------------------------------------
    TTWeeks as

    (SELECT VDC_Cal_Year as REMSYear, [VDC_week_no] as WholeYearWeekNo, WeekCommence, ROW_NUMBER() OVER(PARTITION BY vdc_cal_year ORDER BY VDC_week_no) as TermTimeWeekNo
    FROM weeks)

    ---Main---------------------------------------------------------------------
    SELECT    

    s.STYR_Primary_Programme AS PrimaryProgramme, 
    CASE WHEN s.STYR_Age_end_Aug < 16 THEN '1416' WHEN STYR_Age_end_Aug < 19 THEN '1618' ELSE '19+' END AgeBand,
    rg.REGT_Year, 
    RTRIM(ph.PRPH_ML1) AS PRPH_ML1, 
    q.GNCD_Description AS [Curriculum Area], 
    RTRIM(ph.PRPH_ML2) AS Section, 
    q.GNCD_Description AS Section_Name, 
    lEFT(q.GNCD_Description, 3) AS Dept, 
    RTRIM(rg.REGT_Provision_Code) as REGT_Provision_Code, 
    RTRIM(ph.PRPH_Title) as PRPH_Title, 
    rg.REGT_Student_ID, STEN_Student_ID, STEN_Funding_Stream,  
    rs.REGS_Session_No, 
    RTRIM(rh.REGH_Class_Register) as Class_Register, 
    RTRIM(rh.REGH_Register_Title) as Register_Title, 
    CASE WHEN rh.REGH_Day = '1' THEN 'Sunday' WHEN rh.REGH_Day = '2' THEN 'Monday' 
    WHEN rh.REGH_Day = '3' THEN 'Tuesday' WHEN rh.REGH_Day = '4' THEN 'Wednesday'
    WHEN rh.REGH_Day = '5' THEN 'Thursday' WHEN rh.REGH_Day = '6' THEN 'Friday' 
    WHEN rh.REGH_Day = '7' THEN 'Saturday' END AS Register_Day, 
    rh.[REGH_Start_Time] as StartTime, rh.[REGH_End_Time] as EndTime,
        CASE WHEN (rg.REGT_Provision_Code LIKE '27%' 
    OR rg.REGT_Provision_Code LIKE 'MA27%' OR
    rg.REGT_Provision_Code LIKE 'FS%') 
    THEN 'FunctionalSkill' 
    WHEN rg.REGT_Provision_Code LIKE '16%' THEN 'GCSE' 
    WHEN rg.REGT_Provision_Code LIKE '%/F%' OR
    rg.REGT_Provision_Code LIKE '%/D%' OR
    rg.REGT_Provision_Code LIKE '%/E%' OR
    rg.REGT_Provision_Code LIKE '%/X%' THEN 'Main' ELSE 'Addition' END AS CourseType, 
    CASE WHEN ISNULL(ra.RGAT_Present,'X') IN ('N', 'Y','X') and rs.REGS_Session_Date<GETDATE()
    THEN rs.REGS_Duration - ISNULL(rd.REGD_Mins_Late,0) ELSE 0 END AS Mins_Poss, 
    CASE WHEN ISNULL(ra.RGAT_Present,'X') = 'Y' AND 
    rs.REGS_Session_Date<GETDATE() THEN 
    rs.REGS_Duration - rd.REGD_Mins_Late ELSE 0 END AS Mins_Att, 
    CASE WHEN ra.RGAT_Present = 'Y' AND rs.REGS_Session_Date<GETDATE() THEN 
    rd.REGD_Mins_Late ELSE 0 END AS Mins_Late, 
    rs.[REGS_Session_Date] as Session_Date, TermTimeWeekNo,
    rh.REGH_ISN, ph.PRPH_ISN, st.STUD_Surname, st.STUD_Forename_1, rd.REGD_Attendance_Mark,
    rg.REGT_start_date, rg.REGT_End_date, WeekCommence,
    STFM_LearnFAMCode,
    CASE 
    WHEN STFM_LearnFAMCode=1 THEN '14-15 year old learner is eligible for free meals'
    WHEN STFM_LearnFAMCode=2 THEN '16-19 year old learner is eligible for and in receipt of free meals'
    ELSE 'N/A'
    END AS FreeMealsIndicator,
    CASE 
    WHEN STFM_LearnFAMCode=36   THEN 'Care to Learn (C2L) (EFA funded only)'
    WHEN STFM_LearnFAMCode=55   THEN '16-19 Bursary Fund - learner member of a vulnerable group (EFA funded only)'  
    WHEN STFM_LearnFAMCode=56   THEN '16-19 Bursary Fund - learner awarded discretionary bursary (EFA funded only)' 
    WHEN STFM_LearnFAMCode=57   THEN 'Residential support (EFA funded only)'
    WHEN STFM_LearnFAMCode=58   THEN '19+ Hardship (Skills Funding Agency funded learners only)'    
    WHEN STFM_LearnFAMCode=59   THEN '20+ Childcare (Skills Funding Agency funded learners only)'   
    WHEN STFM_LearnFAMCode=60   THEN 'Residential Access Fund (Skills Funding Agency funded learners only)' 
    WHEN STFM_LearnFAMCode IN (61, 62, 63, 64, 65) THEN 'Unassigned'    
    ELSE 'N/A'
    END AS Bursary, 
    CASE 
    WHEN st.STUD_Gender='M' THEN 'Male'
    WHEN st.STUD_Gender='F' THEN 'Female'
    END AS Gender, 
    CASE 
    WHEN st.STUD_Ethnicity= 31 THEN 'White - English / Welsh / Scottish / Northern Irish / British'
    WHEN st.STUD_Ethnicity= 32 THEN 'White - Irish'
    WHEN  st.STUD_Ethnicity= 33 THEN 'White - Gypsy or Irish Traveller'
    WHEN  st.STUD_Ethnicity= 34 THEN 'White - Any Other White background'
    WHEN  st.STUD_Ethnicity= 35 THEN 'Mixed / Multiple Ethnic group - White and Black Caribbean'
    WHEN  st.STUD_Ethnicity= 36 THEN 'Mixed / Multiple Ethnic group - White and Black African'
    WHEN  st.STUD_Ethnicity= 37 THEN 'Mixed / Multiple Ethnic group - White and Asian'
    WHEN  st.STUD_Ethnicity= 38 THEN 'Mixed / Multiple Ethnic group - Any Other Mixed / multiple ethnic background'
    WHEN  st.STUD_Ethnicity= 39 THEN 'Asian / Asian British - Indian'
    WHEN  st.STUD_Ethnicity= 40 THEN 'Asian/ Asian British - Pakistani'
    WHEN  st.STUD_Ethnicity= 41 THEN 'Asian / Asian British - Bangladeshi'
    WHEN  st.STUD_Ethnicity= 42 THEN 'Asian / Asian British - Chinese'
    WHEN  st.STUD_Ethnicity= 43 THEN 'Asian / Asian British - Any other Asian background'
    WHEN  st.STUD_Ethnicity= 44 THEN 'Black / African / Caribbean / Black British - African'
    WHEN  st.STUD_Ethnicity= 45 THEN 'Black / African / Caribbean / Black British - Caribbean'
    WHEN  st.STUD_Ethnicity= 46 THEN 'Black / African / Caribbean / Black British - Any other Black / African / Caribbean background'
    WHEN  st.STUD_Ethnicity= 47 THEN 'Other ethnic group - Arab'
    WHEN  st.STUD_Ethnicity= 98 THEN 'Any Other'
    WHEN  st.STUD_Ethnicity= 99 THEN 'Not provided'
    END AS Ethnicity,
    CASE 
    WHEN g.GNUC_Flag_1 = 1 THEN 'Yes'
    WHEN g.GNUC_Flag_1 = 0 THEN 'No'
    END AS [Looked After Child],
    CASE 
    WHEN SUB1.GNUC_Type = 'STYR' THEN 'Yes'
    WHEN SUB1.GNUC_Type != 'STYR' THEN 'No'
    END AS ALS

    INTO dbo.WarehouseAttendance_New   

    FROM        
    sql10.ng.dbo.REGTrgstudt rg
    INNER JOIN
    sql10.ng.dbo.REGSrgsessn rs ON rg.REGT_REGH_ISN = rs.REGS_REGH_ISN  
    INNER JOIN sql10.ng.dbo.Vdaily_calender
    ON rs.REGS_Session_Date=VDC_day_date 
    INNER JOIN TTWeeks on rg.REGT_year=TTWeeks.REMSYear
    and VDC_week_no=WholeYearWeekNo and VDC_cal_year=REMSYear
    INNER JOIN
    sql10.ng.dbo.REGHrghdr rh ON rh.REGH_ISN = rg.REGT_REGH_ISN 
    INNER JOIN
    sql10.ng.dbo.PRphprovisionHeader ph ON rg.REGT_Provision_Code = ph.PRPH_Code 
    INNER JOIN
    sql10.ng.dbo.GNCDgncodes AS ML2 ON ML2.GNCD_General_Code = PRPH_ML2 AND ML2.GNCD_Code_Type = 'M2' 
    INNER JOIN
    sql10.ng.dbo.STEN ON STEN_Student_ID = rg.REGT_Student_ID AND STEN_Provision_Code = rg.REGT_Provision_Code AND
    STEN_Provision_Instance = rg.REGT_Provision_Instance 
    LEFT OUTER JOIN  sql10.ng.dbo.STFMLearnerFAM ON STFM_Year = STEN_Year AND STFM_Student_ID = STEN_Student_ID 
    INNER JOIN
    sql10.ng.dbo.STYRstudentYR s ON s.STYR_Student_ID = rg.REGT_Student_ID AND s.STYR_Year = rg.REGT_Year   
    INNER JOIN (SELECT GNUC_Type, GNUC_Year, GNUC_Entity_ISN FROM sql10.ng.dbo.GNUCustom 
    WHERE (GNUC_Year IN ('2013', '2014', '2015'))) AS SUB1 ON rg.REGT_Year = SUB1.GNUC_Year 
    -- AND STYR_ISN = SUB1.GNUC_Entity_ISN
    INNER JOIN
    (SELECT     ACYR_College_Year
    FROM          sql10.ng.dbo.ACYR
    WHERE      (ACYR_College_Year in (2015, 2014, 2013, 2012))) AS SUB ON REGT_Year = SUB.ACYR_College_Year
    INNER JOIN sql10.ng.dbo.STUDStudent st on s.STYR_Student_ID=st.STUD_Student_ID
    INNER JOIN sql10.ng.dbo.GNUCustom g ON STUD_ISN = g.GNUC_Entity_ISN AND g.GNUC_Type = 'STUD'       
    INNER JOIN sql10.ng.dbo.REGDropin rd ON rg.REGT_REGH_ISN = rd.REGD_REGH_ISN AND rg.REGT_Student_ID = rd.REGD_Student_ID 
    AND rd.REGD_Session_No = rs.REGS_Session_No
    INNER JOIN
    sql10.ng.dbo.RGATAttendance ra ON rd.REGD_Attendance_Mark = ra.RGAT_Attendance_Code 
    INNER JOIN
    sql10.ng.dbo.PRPIProvisionInstance AS pit ON pit.PRPI_Code = rg.REGT_Provision_Code 
    and pit.prpi_instance = REGT_Provision_Instance
    INNER JOIN
    (SELECT     GNCD_General_Code, GNCD_Description
    FROM          sql10.ng.dbo.GNCDgncodes
    WHERE      (GNCD_Code_Type = 'M2')) AS q ON q.GNCD_General_Code =  RTrim(PRPH_ML2) 
    COMMIT
    END

    GO

我在执行存储过程作为将数据填充到表中的作业时遇到了锁定问题,因此我需要知道如何改进查询计划。

谢谢, 阿鲁娜

【问题讨论】:

EXEC('...') AT [SQL10.NG] 清除案例...在程序开始时,将 STFM_LearnFAMCode 和 STUD_Ethnicity 的 ID 和名称放入临时表中,并在您的查询之后加入它们...加入将比案例快得多,因为您可以将索引放在临时表上。这是我要做的第一件事 使用 openquery 与链接服务器数据库对话。 【参考方案1】:

免责声明:我查看了查询,我没有详细阅读。

查询过大。一旦它们达到一定的大小,SQL 优化器就会在生成有效的查询计划时出现问题,而在查询中使用“链接服务器表”只会加剧问题。我会将这个令人难以置信的复杂、过于复杂且通常丑陋的查询分解为多个步骤。

首先对远程服务器或服务器运行数据检索,将返回的数据存储在本地,可能在#temp 表中。 (如果返回的数据很多,请在 #temp 表上建立索引。)接下来,将这些表连接到相关的本地表。

返回的实际列似乎包含嵌入在案例语句中的复杂业务规则。它们可能包含在第二个查询中,或者,如果足够复杂,它们可能会调用第三个查询,第二个查询的结果存储在第二个 #temp 表中。

可以通过 SQL 优化器或通过巧妙的实现更好地优化较小的代码块。太大就是太大。

【讨论】:

【参考方案2】:
ALTER PROCEDURE [dbo].[sp_WarehouseAttendance_New]
AS
BEGIN

    SET NOCOUNT ON;

    ...

    DECLARE @sql NVARCHAR(MAX) = '

    SELECT s.STYR_Primary_Programme AS PrimaryProgramme,
           /*....*/
           SUB1.GNUC_Type
    FROM dbo.REGTrgstudt rg
    JOIN dbo.REGSrgsessn rs ON rg.REGT_REGH_ISN = rs.REGS_REGH_ISN
    JOIN dbo.Vdaily_calender ON rs.REGS_Session_Date = VDC_day_date
    JOIN (
        SELECT VDC_Cal_Year AS REMSYear, [VDC_week_no] AS WholeYearWeekNo, WeekCommence, ROW_NUMBER() OVER (PARTITION BY vdc_cal_year ORDER BY VDC_week_no) AS TermTimeWeekNo
        FROM (
            SELECT VDC_week_no, VDC_cal_year, MIN(VDC_day_date) AS WeekCommence
            FROM dbo.Vdaily_calender
            WHERE VDC_avail = ''T''
            GROUP BY VDC_week_no, VDC_cal_year
        ) t
    ) ON rg.REGT_year = TTWeeks.REMSYear
        AND VDC_week_no = WholeYearWeekNo
        AND VDC_cal_year = REMSYear
    JOIN dbo.REGHrghdr rh ON rh.REGH_ISN = rg.REGT_REGH_ISN
    JOIN dbo.PRPHProvisionHeader ph ON rg.REGT_Provision_Code = ph.PRPH_Code
    JOIN dbo.GNCDgncodes AS ML2 ON ML2.GNCD_General_Code = PRPH_ML2
        AND ML2.GNCD_Code_Type = ''M2''
    JOIN dbo.STEN ON STEN_Student_ID = rg.REGT_Student_ID
        AND STEN_Provision_Code = rg.REGT_Provision_Code
        AND STEN_Provision_Instance = rg.REGT_Provision_Instance
    LEFT JOIN dbo.STFMLearnerFAM ON STFM_Year = STEN_Year
        AND STFM_Student_ID = STEN_Student_ID
    INNER JOIN dbo.STYRstudentYR s ON s.STYR_Student_ID = rg.REGT_Student_ID
        AND s.STYR_Year = rg.REGT_Year
    JOIN (
        SELECT GNUC_Type, GNUC_Year, GNUC_Entity_ISN
        FROM dbo.GNUCustom
        WHERE (GNUC_Year IN (''2013'', ''2014'', ''2015''))
    ) AS SUB1 ON rg.REGT_Year = SUB1.GNUC_Year
    JOIN (
        SELECT ACYR_College_Year
        FROM dbo.ACYR
        WHERE (ACYR_College_Year IN (2015, 2014, 2013, 2012))
    ) AS SUB ON REGT_Year = SUB.ACYR_College_Year
    JOIN dbo.STUDStudent st ON s.STYR_Student_ID = st.STUD_Student_ID
    JOIN dbo.GNUCustom g ON STUD_ISN = g.GNUC_Entity_ISN
        AND g.GNUC_Type = ''STUD''
    JOIN dbo.REGDropin rd ON rg.REGT_REGH_ISN = rd.REGD_REGH_ISN
        AND rg.REGT_Student_ID = rd.REGD_Student_ID
        AND rd.REGD_Session_No = rs.REGS_Session_No
    JOIN dbo.RGATAttendance ra ON rd.REGD_Attendance_Mark = ra.RGAT_Attendance_Code
    JOIN dbo.PRPIProvisionInstance AS pit ON pit.PRPI_Code = rg.REGT_Provision_Code
        AND pit.prpi_instance = REGT_Provision_Instance
    JOIN (
        SELECT GNCD_General_Code, GNCD_Description
        FROM dbo.GNCDgncodes
        WHERE (GNCD_Code_Type = ''M2'')
    ) AS q ON q.GNCD_General_Code = RTRIM(PRPH_ML2)'

    INSERT INTO dbo.WarehouseAttendance_TEMP
    EXEC(@sql) AT [sql10.ng]

    INSERT INTO dbo.WarehouseAttendance_New
    SELECT ...
    FROM dbo.WarehouseAttendance_TEMP

END               

【讨论】:

【参考方案3】:

尝试在存储过程中为 TTWeeks 创建一个表变量。 使用 Common Table 表达式进行 INNER JOIN(如您的情况),每次都会为每条记录计算它,这会在更大程度上影响性能。

类似这样的:

Declare @TTWeeks as table 
(
 REMSYear int
 ,WholeYearWeekNo int
 ,TermTimeWeekNo int
 ,WeekCommence datetime
 )

我同意上面 Philip Kelley 的回答。与巨大的丑陋查询相比,可以更好地优化更小的代码块。

【讨论】:

创建此表后在哪里可以看到? 像任何其他变量一样在您的存储过程中创建它,然后使用您在 TTWeeks CTE 中使用的查询进行插入。您可以像使用任何其他表一样使用表变量。你应该在你的 sql 中使用 @TTWeeks 而不是 TTWeeks。

以上是关于如何降低通过链接服务器执行的查询的成本的主要内容,如果未能解决你的问题,请参考以下文章

如何对链接服务器执行存储过程?

弃用 AWS 后,我们服务器的年成本降低了 80%

弃用 AWS 后,我们服务器的年成本降低了 80%

弃用 AWS 后,我们服务器的年成本降低了 80%

USQL如何帮爱普新媒降低80%成本,提升50%数据分析速度

降低 SQL 查询的 CPU 成本