我正在使用SQL Server 2012,我有一個查詢的搜索目的,但它需要太長的時間來執行..(30行= 6秒)SQL Server查詢太慢,缺少索引?
我已經檢查了執行計劃,但他沒有建議把索引,我不知道我可以如何加快查詢。
這裏是我的查詢:
SELECT
dbo.tbRV.ID, dbo.tbRV.IDCompagnie,
dbo.tbPatient.Nom + N', ' + dbo.tbPatient.Prenom AS NomPrenomPatient,
dbo.tbRV.DateRV, dbo.tbRV.HeureDebut,
dbo.tbRV.IDRessource,
coalesce(dbo.tbRessource.Nom ,'') + ' ' + coalesce(dbo.tbRessource.Prenom,'') AS NomPrenomRessource,
dbo.tbPatient.NoApp,
dbo.tbPatient.NomEtablissement, dbo.tbPatient.Adresse1,
tbAdresse.Ville, dbo.tbPatient.TelephoneDomicile,
dbo.tbPatient.TelephoneTravail,
dbo.tbPatient.TelephonePortable,
case
when DD.id is not null then DD.description
else dbo.tbRVObjet.IDObjet
end as idobjet,
dbo.tbPatient.Nom_SansAccent, dbo.tbPatient.Prenom_SansAccent,
dbo.tbPatient.Nom, dbo.tbPatient.Prenom,
dbo.tbRV.IDPatient, dbo.tbPatient.NoAssuranceMaladie,
dbo.tbPatient.DateNaissance, dbo.tbRV.Transit, dbo.tbRV.Annuler,
tbAdresse.Pays, tbAdresse.Province, dbo.tbPatient.Adresse2,
dbo.tbPatient.CodePostal,
case
when AA.id is not null and AA.ID = tbrvobjet.ID then 1 else 0 end as onlyFirst
FROM
tbPatient
INNER JOIN
tbRV ON tbPatient.ID = tbRV.IDPatient
INNER JOIN
tbRessource ON (tbRV.IDRessource = tbRessource.ID) AND (tbRV.IDCompagnie = tbRessource.IDCompagnie)
INNER JOIN
tbRVObjet ON tbRV.ID = tbRVObjet.IDRV
OUTER APPLY
dbo.fn_GetFirstIDRVObjet(tbrv.id) AA
OUTER APPLY (SELECT A.ID, A.Description
FROM tbForfaitEntete A
WHERE A.ID = tbRVObjet.IDForfait
AND ISNULL(tbRVObjet.IsForfaitEntete, 0) = 1) DD
OUTER APPLY (SELECT B.Description
FROM tbRVObjet A
JOIN tbForfaitEntete B ON (A.IDForfait = B.ID AND ISNULL(A.IsForfaitEntete, 0) = 1)
WHERE AA.ID = A.id) BB
OUTER APPLY (SELECT tbPaysISO.Pays, tbProvinceISO.Province, tbVilleISO.Ville
FROM tbPaysISO
JOIN tbProvinceISO ON (tbPaysISO.Langue = tbProvinceISO.Langue)
AND (tbPatient.ProvinceISOChar = tbProvinceISO.ProvinceISOChar)
JOIN tbVilleISO ON (tbProvinceISO.Langue = tbVilleISO.Langue)
AND (tbPatient.IdVille = tbVilleISO.IDVille)
WHERE tbPaysISO.Langue = tbpatient.CodeLangue
AND tbPaysISO.PaysISOChar3 = tbpatient.PaysISOChar3) tbAdresse
,這裏是在XML的執行計劃:
編輯:
這裏是我的查詢計劃網址:https://www.brentozar.com/pastetheplan/?id=HkVYeasNW
這裏是我的搜索查詢:
Select
ID, IDPatient, NomPrenomPatient, DateNaissance, NoAssuranceMaladie,
NomEtablissement, Adresse1, Adresse2, NoApp, ville, province, pays,
CodePostal, TelephoneDomicile, TelephoneTravail, TelephonePortable,
DateRV, HeureDebut, IDObjet, IDRessource, NomPrenomRessource,
Annuler, Transit
From
rqAfficheRechercheRV
Where
IDCompagnie = 1
And (isnull(NomPrenomRessource, '') + isnull(NoApp, '') + isnull(NomEtablissement, '') + isnull(Adresse1, '') + isnull(Ville, '') + isnull(TelephoneDomicile, '') + isnull(TelephoneTravail, '') + isnull(TelephonePortable, '') + isnull(IDObjet, '') + isnull(Nom_SansAccent, '') + isnull(Prenom_SansAccent, '') + isnull(Nom, '') + isnull(Prenom, '') + isnull(IDPatient, '') + isnull(NoAssuranceMaladie, '') + isnull(convert(varchar, DateNaissance), '') + isnull(Pays, '') + isnull(Province, '') + isnull(Adresse2, '') + isnull(CodePostal, '')) like '%881-1360%'
And isnull(onlyFirst,0) = 1
Order by
Nom, Prenom, DateRV DESC, HeureDebut ASC
rqAfficheRechercheRV是查詢以上
真的很難讀你在截圖的形式規劃。你可以使用這個:https://www.brentozar.com/pastetheplan/?我假設你在ID字段上有聚簇索引,但Langue和PaysISOChar3字段上的索引是什麼? –
@JacobH我把我的查詢計劃在我的問題感謝information.PaysISOChar3字段這些都是varchar字段。 – alexandre
平時對搜索查詢有where子句,這似乎是在返回表的一切。你在應用程序中過濾嗎? – Jesse