Ich habe diese Abfrage, die ziemlich lang ist, aber eine Where-Klausel hinzufügen, oder Beitritt zu einer Zeichenfolge macht es zusätzliche 2 Sekunden zu laufen. Ich kann nicht herausfinden warum.Beitritt auf Varchar (50) Fremdschlüssel verlangsamt Abfrage
Hier ist die Abfrage in voller Länge:
ALTER PROCEDURE [dbo].[RespondersByPracticeID]
@practiceID int = null,
@activeOnly bit = 1
AS
BEGIN
SET NOCOUNT ON;
select
isnull(sum(isResponder),0) as [Responders]
,isnull(count(*) - sum(isResponder),0) as [NonResponders]
,isnull((select
count(p.patientID)
from patient p
inner join practice on practice.practiceid = p.practiceid
inner join [lookup] l on p.dosing = l.lookupid and l.lookupid = 'da_ncd'
where
p.practiceID = isnull(@practiceID, p.practiceID)
and p.active = case @activeOnly when 1 then 1 else p.active end
) - (isnull(sum(isResponder),0) + isnull(count(*) - sum(isResponder),0)),0)
as [Undetermined]
from (
select
v.patientID
,firstVisit.hbLevel as startHb
,maxHbVisit.hblevel as maxHb
, case when (maxHbVisit.hblevel - firstVisit.hbLevel >= 1) then 1 else 0 end as isResponder
,count(v.patientID) as patientCount
from patient p
inner join visit v on v.patientid = v.patientid
inner join practice on practice.practiceid = p.practiceid
inner join [lookup] l on p.dosing = l.lookupid and l.lookupid = 'da_ncd'
inner join (
SELECT
p.PatientID
,v.VisitID
,v.hblevel
,v.VisitDate
FROM Patient p
INNER JOIN Visit v ON p.PatientID = v.PatientID
WHERE
v.VisitDate = (
SELECT MIN(VisitDate)
FROM Visit
WHERE PatientId = p.PatientId
)
) firstVisit on firstVisit.patientID = v.patientID
inner join (
select
p.patientID
,max(v.hbLevel) as hblevel
from Patient p
INNER JOIN Visit v ON p.PatientID = v.PatientID
group by
p.patientID
) MaxHbVisit on maxHbVisit.patientid = v.patientId
where
p.practiceID = isnull(@practiceID, p.practiceID)
and p.active = case @activeOnly when 1 then 1 else p.active end
group by
v.patientID
,firstVisit.hbLevel
,maxHbVisit.hblevel
having
datediff(
d,
dateadd(
day
,-DatePart(
dw
,min(v.visitDate)
) + 1
,min(v.visitDate)
)
, max(v.visitDate)
) >= (7 * 8) -- Eight weeks.
) responders
END
Die Linie, die es verlangsamt ist:
inner join [lookup] l on p.dosing = l.lookupid and l.lookupid = 'da_ncd'
Auch an die where
Klausel bewegt die gleiche Wirkung hat:
where p.dosing = 'da_ncd'
Andernfalls wird die Abfrage fast sofort ausgeführt. >.<
ist l.lookupid indiziert? – sfossen
Ja ist es der Primärschlüssel – Shawn
ist es möglich, einen int-Basis-Primärschlüssel und Foreign-Schlüssel zu verwenden? – sfossen