慢速报告T-SQL

时间:2016-02-23 08:39:05

标签: sql sql-server performance tsql report

我已经报告说,一开始工作正常。但是当有更多数据需要大约10分钟时。我知道左边有一些东西,但我不知道我可以改变什么。

有人可以帮助我吗?如果您需要更多详细信息,请写下来!

declare @opis varchar(30) = ''
declare @rok int=2016
declare @miesiac int=2

;with cte as 
(
    SELECT DISTINCT 
        ROW_NUMBER() over (Partition by isnull(emp.emp_name,emp1.emp_name),isnull(doc_documentdate,test.tmd_fulldatekey)  ORDER BY doc_documentdate ) as [s],  
        isnull(emp.emp_name,emp1.emp_name) Kod, 
        elm_id,
        isnull(emp.emp_id,test.emp_id) id,
        isnull(emp.Emp_Surname,emp1.Emp_Surname) Nazwisko, 
        isnull(emp.Emp_FirstName,emp1.emp_firstname) Imie, 
        CONVERT(VARCHAR(10),isnull(Doc_DocumentDate,test.tmd_fulldatekey),120) Data,
        case when Doc_DocumentDate=ttc_date and ttc_isholiday=0 then 'S' 
        when doc_documentdate=ttc_date and ttc_isholiday=6 or ttc_isholiday=7 or ELM_DecimalColumn1=0 then 'W' 
        when ELM_DecimalColumn1 is null then 'w'
        else 'P' end Typdnia,
        isnull(CONVERT(VARCHAR(8),elm_datecolumn1,108),'00:00:00') GodzOd,
        isnull(CONVERT(VARCHAR(8),elm_datecolumn2,108),'00:00:00') GodzDo,
        case when ELM_decimalcolumn1>0 then 'praca podstawowa' else 'brak' end Strefa,
        @opis Opis,
        isnull(elm_shortstringcolumn6,'brak') Wydzial,
        isnull(ELM_ShortStringColumn5,'0') Wydzial_adres_wezla,
        isnull(a.dic_svalue,' brak') Projekt,
        isnull(b.DIC_ShortStringColumn1,'1') Projekt_adres_wezla
    FROM Employees emp
    LEFT JOIN elements on emp_id=ELM_EmpID and ELM_FflID=200 
    LEFT JOIN documents on elm_objectid=doc_id and Doc_DdfID=2
    LEFT JOIN dictionaries a on ELM_IntColumn2=a.DIC_ID
    LEFT JOIN Dictionaries b on elm_intcolumn2=b.DIC_ID
    LEFT JOIN ttscalendar on ttc_date=Doc_DocumentDate
    RIGHT JOIN
    (
        select distinct emp_id, tmd_fulldatekey 
        from Employees
        INNER JOIN elements on emp_id=elm_empid 
        CROSS JOIN Dim_TimeDay 
        WHERE year(tmd_fulldatekey)=@rok and month(tmd_fulldatekey)=@Miesiac
    ) test on tmd_fulldatekey=doc_documentdate and emp.emp_id=test.Emp_ID
    INNER JOIN Employees emp1 on emp1.Emp_ID=test.Emp_ID
 ) 
 SELECT 
    isnull(cte1.kod,'') Kod, isnull(cte1.nazwisko,'') Nazwisko, isnull(cte1.imie,'') Imie, isnull(cte1.data,'')Data, isnull(cte1.TypDnia,'')Typdnia, isnull(cte1.GodzOd,'00:00')W1_GodzOd, isnull(cte1.godzdo,'00:00')W1_GodzDo, isnull(cte1.strefa,'brak')W1_Strefa, isnull(cte1.opis,'')W1_Opis, isnull(cte1.wydzial,'brak')W1_Wydzial, isnull(cte1.wydzial_adres_wezla,'0')W1_Wydzial_adres_wezla, isnull(cte1.Projekt,'brak')W1_Projekt, isnull(cte1.Projekt_adres_wezla,'1')W1_Projekt_adres_wezla,
    isnull(cte2.GodzOd,'')W2_GodzOd, isnull(cte2.godzdo,'')W2_GodzDo, isnull(cte2.strefa,'')W2_Strefa, isnull(cte2.opis,'')W2_Opis, isnull(cte2.wydzial,'')W2_Wydzial, isnull(cte2.wydzial_adres_wezla,'')W2_Wydzial_adres_wezla, isnull(cte2.Projekt,'')W2_Projekt, isnull(cte2.Projekt_adres_wezla,'')W2_Projekt_adres_wezla,
    isnull(cte3.GodzOd,'')W3_GodzOd, isnull(cte3.godzdo,'')W3_GodzDo, isnull(cte3.strefa,'')W3_Strefa, isnull(cte3.opis,'')W3_Opis, isnull(cte3.wydzial,'')W3_Wydzial, isnull(cte3.wydzial_adres_wezla,'')W3_Wydzial_adres_wezla, isnull(cte3.Projekt,'')W3_Projekt, isnull(cte3.Projekt_adres_wezla,'')W3_Projekt_adres_wezla,
    isnull(cte4.GodzOd,'')W4_GodzOd, isnull(cte4.godzdo,'')W4_GodzDo, isnull(cte4.strefa,'')W4_Strefa, isnull(cte4.opis,'')W4_Opis, isnull(cte4.wydzial,'')W4_Wydzial, isnull(cte4.wydzial_adres_wezla,'')W4_Wydzial_adres_wezla, isnull(cte4.Projekt,'')W4_Projekt, isnull(cte4.Projekt_adres_wezla,'')W4_Projekt_adres_wezla,
    isnull(cte5.GodzOd,'')W5_GodzOd, isnull(cte5.godzdo,'')W5_GodzDo, isnull(cte5.strefa,'')W5_Strefa, isnull(cte5.opis,'')W5_Opis, isnull(cte5.wydzial,'')W5_Wydzial, isnull(cte5.wydzial_adres_wezla,'')W5_Wydzial_adres_wezla, isnull(cte5.Projekt,'')W5_Projekt, isnull(cte5.Projekt_adres_wezla,'')W5_Projekt_adres_wezla,
    isnull(cte6.GodzOd,'')W6_GodzOd, isnull(cte6.godzdo,'')W6_GodzDo, isnull(cte6.strefa,'')W6_Strefa, isnull(cte6.opis,'')W6_Opis, isnull(cte6.wydzial,'')W6_Wydzial, isnull(cte6.wydzial_adres_wezla,'')W6_Wydzial_adres_wezla, isnull(cte6.Projekt,'')W6_Projekt, isnull(cte6.Projekt_adres_wezla,'')W6_Projekt_adres_wezla
from cte cte1
LEFT JOIN cte cte2 on cte1.Kod=cte2.kod and cte1.data=cte2.data and cte2.s=2 
LEFT JOIN cte cte3 on cte2.Kod=cte3.kod and cte2.data=cte3.data and cte3.s=3
LEFT JOIN cte cte4 on cte3.Kod=cte4.kod and cte3.data=cte4.data and cte4.s=4
LEFT JOIN cte cte5 on cte4.Kod=cte5.kod and cte4.data=cte5.data and cte5.s=5
LEFT JOIN cte cte6 on cte5.Kod=cte6.kod and cte5.data=cte6.data and cte6.s=6
WHERE cte1.s=1 and year(cte1.data)=@rok and month(cte1.data)=@Miesiac
ORDER BY cte1.kod,cte1.data

0 个答案:

没有答案