Skip to content

Commit 50881c6

Browse files
committed
prov dupe query efficiency
1 parent b410912 commit 50881c6

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SdcSchoolCollectionStudentRepository.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,33 +318,33 @@ HAVING COUNT(stud.assignedStudentId) > 1)
318318
List<SdcSchoolCollectionStudentLightEntity> findAllInProvinceDuplicateStudentsInSdcSchoolCollection(UUID collectionID, UUID sdcSchoolCollectionID);
319319

320320
@Query(value = """
321-
WITH district_students AS MATERIALIZED (
321+
WITH province_dupes AS MATERIALIZED (
322322
SELECT s.assigned_student_id
323323
FROM sdc_school_collection_student s
324324
JOIN sdc_school_collection sc ON sc.sdc_school_collection_id = s.sdc_school_collection_id
325325
WHERE sc.collection_id = :collectionID
326-
AND sc.sdc_district_collection_id = :sdcDistrictCollectionID
327326
AND s.sdc_school_collection_student_status_code != 'DELETED'
328327
AND s.assigned_student_id IS NOT NULL
328+
GROUP BY s.assigned_student_id
329+
HAVING COUNT(*) > 1
329330
),
330-
province_dupes AS MATERIALIZED (
331-
SELECT s.assigned_student_id
331+
district_dupes AS MATERIALIZED (
332+
SELECT DISTINCT s.assigned_student_id
332333
FROM sdc_school_collection_student s
333334
JOIN sdc_school_collection sc ON sc.sdc_school_collection_id = s.sdc_school_collection_id
334335
WHERE sc.collection_id = :collectionID
336+
AND sc.sdc_district_collection_id = :sdcDistrictCollectionID
335337
AND s.sdc_school_collection_student_status_code != 'DELETED'
336338
AND s.assigned_student_id IS NOT NULL
337-
AND s.assigned_student_id IN (SELECT assigned_student_id FROM district_students)
338-
GROUP BY s.assigned_student_id
339-
HAVING COUNT(*) > 1
339+
AND s.assigned_student_id IN (SELECT assigned_student_id FROM province_dupes)
340340
)
341341
SELECT s.sdc_school_collection_student_id
342342
FROM sdc_school_collection_student s
343343
JOIN sdc_school_collection sc ON sc.sdc_school_collection_id = s.sdc_school_collection_id
344344
WHERE sc.collection_id = :collectionID
345345
AND s.sdc_school_collection_student_status_code != 'DELETED'
346346
AND s.assigned_student_id IS NOT NULL
347-
AND s.assigned_student_id IN (SELECT assigned_student_id FROM province_dupes)
347+
AND s.assigned_student_id IN (SELECT assigned_student_id FROM district_dupes)
348348
""", nativeQuery = true)
349349
List<UUID> findAllInProvinceDuplicateStudentIdsInSdcDistrictCollection(
350350
UUID collectionID,

0 commit comments

Comments
 (0)