Skip to content

Commit fefefbb

Browse files
authored
fix: data export - replace O(n²) org unit descendants cross-join with path index scan [DHIS2-21490] (#23880)
* fix: SQL to add children for all direct matches * fix: unit test assert
1 parent 8bcb35b commit fefefbb

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/datavalue/hibernate/HibernateDataExportStore.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,9 @@ ou_ids AS (
174174
),
175175
ou_with_descendants_ids AS (
176176
SELECT DISTINCT ou.organisationunitid
177-
FROM organisationunit ou
178-
LEFT JOIN organisationunit parent_ou ON (ou.path LIKE parent_ou.path || '%')
179-
WHERE ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids)
180-
OR parent_ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids)
177+
FROM ou_ids
178+
JOIN organisationunit root USING (organisationunitid)
179+
JOIN organisationunit ou ON ou.path LIKE root.path || '%'
181180
)
182181
SELECT
183182
de.uid AS deid,

dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datavalue/hibernate/DataExportQueryBuilderTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,9 @@ ou_ids AS (
298298
),
299299
ou_with_descendants_ids AS (
300300
SELECT DISTINCT ou.organisationunitid
301-
FROM organisationunit ou
302-
LEFT JOIN organisationunit parent_ou ON (ou.path LIKE parent_ou.path || '%')
303-
WHERE ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids)
304-
OR parent_ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids)
301+
FROM ou_ids
302+
JOIN organisationunit root USING (organisationunitid)
303+
JOIN organisationunit ou ON ou.path LIKE root.path || '%'
305304
)
306305
SELECT
307306
de.uid AS deid,
@@ -391,10 +390,9 @@ ou_ids AS (
391390
),
392391
ou_with_descendants_ids AS (
393392
SELECT DISTINCT ou.organisationunitid
394-
FROM organisationunit ou
395-
LEFT JOIN organisationunit parent_ou ON (ou.path LIKE parent_ou.path || '%')
396-
WHERE ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids)
397-
OR parent_ou.organisationunitid IN (SELECT organisationunitid FROM ou_ids)
393+
FROM ou_ids
394+
JOIN organisationunit root USING (organisationunitid)
395+
JOIN organisationunit ou ON ou.path LIKE root.path || '%'
398396
)
399397
SELECT
400398
de.uid AS deid,

0 commit comments

Comments
 (0)