SQL과 관련하여 나는 상대적으로 경험이 없습니다. 나는 CROSS APPLY
을 사용하는 것이 아래의 SQL에서 가장 좋은 옵션인지 궁금합니다.CROSS가 가장 좋은 방법인가요 아니면 EXISTS 또는 하위 쿼리가있을 때 가능합니까?
나는 또한 작품을 복제한다고 생각합니다. 이것은 효과가 있고 몇 분이 걸리지 만 제 느낌은 더 잘할 수 있다는 것입니다.
내가보고있는 3 개의 주 테이블은 꽤 크고 각각 2 백만 행을보고 있습니다. 개별 insert 문은 각각 약 7 - 15K 행으로 되돌아갑니다.
DECLARE @Master TABLE
(
heyno NVARCHAR(12),
postcodestartdttm DATE,
postcodeenddttm DATE,
lzohistorypostcode NVARCHAR(25),
biactivitypostcode NVARCHAR(25),
activityenddttm DATE
);
INSERT INTO @Master
SELECT p.pasid,
par.startdttm,
par.enddttm,
pa.postcode AS LZOHistoryPostcode,
t.postcode AS BIActivityPostcode,
t.admitdate
FROM HEALTHBI.DBO.[lzo_patientaddressrole] AS par
INNER JOIN HEALTHBI.DBO.[lzo_patientaddress] AS pa
ON par.[addressoid] = pa.[oid]
AND pa.[status] = 'A'
INNER JOIN HEALTHBI.DBO.lzo_patient AS p
ON par.identifyingoid = p.oid
---
CROSS APPLY (SELECT i.admitdate,
i.postcode
FROM HEALTHBI_VIEWS.DBO.ip_admission i
WHERE i.patientoid = p.oid
AND i.admitdate > par.startdttm
AND i.admitdate >= '01 APRIL 2010'
AND i.admitdate < ISNULL(par.enddttm, '31-dec-4712')
AND par.[status] = 'A'
AND par.[identifyingtype] = 'Patient'
AND par.[rotypcode] = 'CC_USUALADD'
AND pa.[adtypcode] = 'Address'
AND pa.postcode <> i.postcode) AS t
INSERT INTO @Master
SELECT p.pasid,
par.startdttm,
par.enddttm,
pa.postcode AS LZOHistoryPostcode,
t.postcode AS BIActivityPostcode,
t.apptstartdate
FROM HEALTHBI.DBO.[lzo_patientaddressrole] AS par
INNER JOIN HEALTHBI.DBO.[lzo_patientaddress] AS pa
ON par.[addressoid] = pa.[oid]
AND pa.[status] = 'A'
INNER JOIN HEALTHBI.DBO.lzo_patient AS p
ON par.identifyingoid = p.oid
CROSS APPLY (SELECT i.apptstartdate,
i.postcode
FROM HEALTHBI_VIEWS.DBO.op_appointment i
WHERE i.patientoid = p.oid
AND i.apptstartdate > par.startdttm
AND i.apptstartdate >= '01 APRIL 2010'
AND i.apptstartdate < ISNULL(par.enddttm, '31-dec-4712')
AND par.[status] = 'A'
AND par.[identifyingtype] = 'Patient'
AND par.[rotypcode] = 'CC_USUALADD'
AND pa.[adtypcode] = 'Address'
AND pa.postcode <> i.postcode) AS t
INSERT INTO @Master
SELECT p.pasid,
par.startdttm,
par.enddttm,
pa.postcode AS LZOHistoryPostcode,
t.postcode AS BIActivityPostcode,
t.attenddate
FROM HEALTHBI.DBO.[lzo_patientaddressrole] AS par
INNER JOIN HEALTHBI.DBO.[lzo_patientaddress] AS pa
ON par.[addressoid] = pa.[oid]
AND pa.[status] = 'A'
INNER JOIN HEALTHBI.DBO.lzo_patient AS p
ON par.identifyingoid = p.oid
CROSS APPLY (SELECT i.attenddate,
i.postcode
FROM HEALTHBI_VIEWS.DBO.ed_attendance i
WHERE i.patientoid = p.oid
AND i.attenddate > par.startdttm
AND i.attenddate >= '01 APRIL 2010'
AND i.attenddate < ISNULL(par.enddttm, '31-dec-4712')
AND par.[status] = 'A'
AND par.[identifyingtype] = 'Patient'
AND par.[rotypcode] = 'CC_USUALADD'
AND pa.[adtypcode] = 'Address'
AND pa.postcode <> i.postcode) AS t
SELECT m.heyno,
m.lzohistorypostcode,
m.biactivitypostcode,
d.startofweek
FROM @Master m
INNER JOIN HEALTHBI_VIEWS.DBO.date_reference AS d
ON m.activityenddttm = d.datevalue
GROUP BY m.heyno,
m.lzohistorypostcode,
m.biactivitypostcode,
d.startofweek
ORDER BY m.heyno
왜 'LEFT JOIN'입니까? – Ben
주 결합이 PK에 대해 1-1이기 때문입니다. – Simon
그래서? 그게 무슨 상관이야? – Ben