我有我們需要進行性能優化的客戶提供的這個sql查詢。我從哪裏開始優化此SQL查詢?
是否有任何人可以點我在正確的方向在哪裏開始尋找優化下面的查詢?
我的本地機器上的查詢需要約6-7秒,但對用戶it's約30秒,它執行在MSSQL 2008R2
謝謝!
var query = @"
DECLARE @SearchString nvarchar(250)
set @SearchString = '1811820001'
;with BaseSelectorCTE (ID) as
(
SELECT ID FROM BaseCases b
where
b.CPR like @SearchString
OR (b.FirstName + ' ' + b.LastName) like @SearchString
OR b.CustomerInfo_InstitutionName like @SearchString
UNION
Select ID from FlexjobCase
where Kommune like @SearchString
UNION
Select ID from DisabledAssistantCase
where Kommune like @SearchString
UNION
Select ID from AdultStudentCase
where Kommune like @SearchString
UNION
Select ID from DiseaseCase
where Kommune like @SearchString
UNION
Select ID from MaternityCase
where Kommune like @SearchString
UNION
Select ID from MiscellaneousCase
where Kommune like @SearchString
UNION
Select ID from WageSubsidyCase
where Kommune like @SearchString
UNION
Select w.ID from WageSubsidyCase w inner join JobCenters j on
w.JobcenterID = j.ID
where
j.Name like @SearchString
UNION
Select a.ID from AdultStudentCase a inner join JobCenters j on
a.JobcenterID = j.ID
where
j.Name like @SearchString
)
--
-- Select BaseCases mapped to result type
--
,ResultSelectorCTE AS
(
select
bc.Id as CaseID,
bc.ChildCaseName,
bc.CPR,
bc.FirstName,
bc.LastName,
bc.CustomerInfo_CustomerInfoID as CustomerInfoID,
bc.CustomerInfo_InstitutionName as InstitutionName,
bc.CaseDeadline,
bc.StatusID,
cs.Name as [StatusName],
cs.Owner as [StatusOwner],
bc.MetaData_Updated as [LastChange],
bc.LastActionDay,
,CASE bc.StatusID WHEN 9 THEN 1 ELSE 0 END as SidstePeriodeSoegt
from BaseCases bc
inner join CaseStatus cs ON
bc.StatusID = cs.ID
inner join BaseSelectorCTE bsCTE ON
bc.ID = bsCTE.ID
)
select * from (Select *, ROW_NUMBER() Over(Order By @@version , CASE WHEN StatusID = 9 then 2 ELSE 1 END, CaseDeadline ASC,
SidstePeriodeSoegt)
As rownum from ResultSelectorCTE where 1=1 AND StatusOwner <> 2 AND StatusOwner <> 3
AND SUBSTRING(CPR, 0, 3) BETWEEN 26-08-2014 AND 26-08-2015) As Result
where rownum Between ((1 - 1) * 100 + 1) AND (1 * 100);
從建議的開始,獲取執行計劃。我還建議你給一些背景,如需要多長時間,獲得多少記錄。至少需要一些時間來正確地格式化代碼,以便人們可以輕鬆地閱讀它等。 – Ernesto