1

我有一個表有13,0000條記錄,但沒有索引,我寫了一個有4個左外連接的查詢。提高使用SQL Server 2012的查詢性能?

查詢工作正常,沒有任何問題。我唯一擔心的是性能,需要5-10分鐘才能得出結果。所以我的問題是如何提高查詢的性能?我的第二個問題是我需要添加索引嗎?如果是,我應該添加哪個索引,集羣還是非集羣?

我的查詢是

SELECT Z.* FROM 
(SELECT 
YTD.Specialisation, 
YTD.SpecialisationCode, 
ROUND(COALESCE(Today.Revenue_Today,0),0)Revenue_Today, 
ROUND(COALESCE(MTD.Revenue_MTD,0),0)Revenue_MTD, 
ROUND(COALESCE(YTD.Revenue_YTD,0),0)Revenue_YTD, 
ROUND(COALESCE(DTD.Revenue_DTD,0),0)Revenue_DTD, 

ROUND(COALESCE(Today.Amount1_Today,0),0)Amount1_Today, 
ROUND(COALESCE(MTD.Amount1_MTD,0),0)Amount1_MTD, 
ROUND(COALESCE(YTD.Amount1_YTD,0),0)Amount1_YTD, 
ROUND(COALESCE(DTD.Amount1_DTD,0),0)Amount1_DTD, 

ROUND(COALESCE(Today.Amount2_Today,0),0)Amount2_Today, 
ROUND(COALESCE(MTD.Amount2_MTD,0),0)Amount2_MTD, 
ROUND(COALESCE(YTD.Amount2_YTD,0),0)Amount2_YTD, 
ROUND(COALESCE(DTD.Amount2_DTD,0),0)Amount2_DTD, 

ROUND(COALESCE(Today.Amount3_Today,0),0)Amount3_Today, 
ROUND(COALESCE(MTD.Amount3_MTD,0),0)Amount3_MTD, 
ROUND(COALESCE(YTD.Amount3_YTD,0),0)Amount3_YTD, 
ROUND(COALESCE(DTD.Amount3_DTD,0),0)Amount3_DTD, 

ROUND(COALESCE(Today.Amount4_Today,0),0)Amount4_Today, 
ROUND(COALESCE(MTD.Amount4_MTD,0),0)Amount4_MTD, 
ROUND(COALESCE(YTD.Amount4_YTD,0),0)Amount4_YTD, 
ROUND(COALESCE(DTD.Amount4_DTD,0),0)Amount4_DTD, 

ROUND(COALESCE(Today.Amount5_Today,0),0)Amount5_Today, 
ROUND(COALESCE(MTD.Amount5_MTD,0),0)Amount5_MTD, 
ROUND(COALESCE(YTD.Amount5_YTD,0),0)Amount5_YTD, 
ROUND(COALESCE(DTD.Amount5_DTD,0),0)Amount5_DTD 


FROM 
(select 
a.SpecialisationCode, 
a.Specialisation, 
SUM(a.DoctorFee)Revenue_YTD, 
SUM(a.Amount1)Amount1_YTD, 
SUM(a.Amount2)Amount2_YTD, 
SUM(a.Amount3)Amount3_YTD, 
SUM(a.Amount4)Amount4_YTD, 
SUM(a.Amount5)Amount5_YTD 
from tbl_doctor a 
where FORMAT((CONVERT(smalldatetime,a.BillDate,111)),'yyyy-MM-dd') >= FORMAT((CONVERT(smalldatetime,'2012-04-01',111)),'yyyy-04-01') 
AND FORMAT((CONVERT(smalldatetime,a.BillDate,111)),'yyyy-MM-dd') <= '2012-05-01' 
and a.SpecialisationCode!=0 
and a.Specialisation NOT IN (' ') 
GROUP BY a.SpecialisationCode,a.Specialisation)YTD 



LEFT OUTER JOIN 

(select 
a.SpecialisationCode, 
a.Specialisation, 
SUM(a.DoctorFee)Revenue_DTD, 
SUM(a.Amount1)Amount1_DTD, 
SUM(a.Amount2)Amount2_DTD, 
SUM(a.Amount3)Amount3_DTD, 
SUM(a.Amount4)Amount4_DTD, 
SUM(a.Amount5)Amount5_DTD 
from tbl_doctor a 
where FORMAT((CONVERT(smalldatetime,a.BillDate,111)),'yyyy-MM-dd') >= '2012-04-01' 
    AND FORMAT((CONVERT(smalldatetime,a.BillDate,111)),'yyyy-MM-dd') <= '2012-05-01' 
and a.SpecialisationCode!=0 
and a.Specialisation NOT IN (' ') 
GROUP BY a.SpecialisationCode,a.Specialisation)DTD 
ON DTD.SpecialisationCode=YTD.SpecialisationCode 

LEFT OUTER JOIN 


(select 
a.SpecialisationCode, 
a.Specialisation, 
SUM(a.DoctorFee)Revenue_MTD, 
SUM(a.Amount1)Amount1_MTD, 
SUM(a.Amount2)Amount2_MTD, 
SUM(a.Amount3)Amount3_MTD, 
SUM(a.Amount4)Amount4_MTD, 
SUM(a.Amount5)Amount5_MTD 
from tbl_doctor a 
where FORMAT((CONVERT(smalldatetime,a.BillDate,111)),'yyyy-MM-dd') >= FORMAT((CONVERT(smalldatetime,'2012-05-01',111)),'yyyy-MM-01') 
    AND FORMAT((CONVERT(smalldatetime,a.BillDate,111)),'yyyy-MM-dd') <= FORMAT((CONVERT(smalldatetime,eomonth('2012-05-01'),111)),'yyyy-MM-dd') 
and a.SpecialisationCode!=0 
and a.Specialisation NOT IN (' ') 
GROUP BY a.SpecialisationCode,a.Specialisation)MTD 

ON YTD.SpecialisationCode=MTD.SpecialisationCode 


LEFT OUTER JOIN 

(select 
a.SpecialisationCode, 
a.Specialisation, 
COALESCE(SUM(a.DoctorFee),0)Revenue_Today, 
SUM(a.Amount1)Amount1_Today, 
SUM(a.Amount2)Amount2_Today, 
SUM(a.Amount3)Amount3_Today, 
SUM(a.Amount4)Amount4_Today, 
SUM(a.Amount5)Amount5_Today 
from tbl_doctor a 
where FORMAT((CONVERT(smalldatetime,a.BillDate,111)),'yyyy-MM-dd') = '2012-05-01' 
and a.SpecialisationCode!=0 
and a.Specialisation NOT IN (' ') 
GROUP BY a.SpecialisationCode,a.Specialisation)Today 
ON YTD.SpecialisationCode=Today.SpecialisationCode) z 
order by z.Specialisation 

回答

1

它看起來對我說,你可以解決整個查詢的一個傳球,通過使用條件聚集。

我還沒有使用SQL Server那麼多,所以我並不完全得到日期邏輯,但據我所知,您計算的是不同時間段的金額,就像從今年開始累積的金額一樣,月初,以及今天的金額等等。

對於這樣的查詢,您無論如何都要在年初至今的日期之間用BillDate來觸及所有記錄。您應該可以使用類似於下面的查詢。這個想法是隻有在BillDate處於該時間段內時才計算該金額。

select a.SpecialisationCode 
     ,a.Specialisation 
     ,sum(case when a.BillDate = today then a.Amount1 end) as Amount1_Today 
     ,sum(case when a.BillDate between date 'first-day-in-month' and today then a.Amount1 end) as Amount1_MTD 
     ,sum(a.Amount1) as Amount1_YTD 
    from tbl_doctor 
where a.SpecialisationCode!=0 
    and a.Specialisation NOT IN (' ') 
    and a.BillDate between date 'first day in year' 
         and date 'today' 
group 
    by a.SpecialisationCode 
     ,a.Specialisation; 

讓我知道如果你沒有得到這個工作!