Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert subquery for matching Patient ID into join to avoid performance issue on PostgreSQL #4219

Closed
gunterze opened this issue Sep 13, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@gunterze
Copy link
Member

Convert subquery for matching Patient ID, e.g.:

select s1_0.study_fk, ...
from series s1_0
         join study s2_0 on s2_0.pk = s1_0.study_fk
         join patient p1_0 on p1_0.pk = s2_0.patient_fk
         join dicomattrs a1_0 on a1_0.pk = p1_0.dicomattrs_fk
         left join study_query_attrs q1_0 on s2_0.pk = q1_0.study_fk and q1_0.view_id=?
         join dicomattrs a2_0 on a2_0.pk = s2_0.dicomattrs_fk
         left join metadata m1_0 on m1_0.pk = s1_0.metadata_fk
         left join series_query_attrs q2_0 on s1_0.pk = q2_0.series_fk and q2_0.view_id=?
         join dicomattrs a3_0 on a3_0.pk = s1_0.dicomattrs_fk
where exists(select p2_0.pk
             from patient_id p2_0
             where (p2_0.pat_id=?) and p2_0.patient_fk = p1_0.pk)

into join, e.g.:

select s1_0.study_fk, ...
from series s1_0
         join study s2_0 on s2_0.pk = s1_0.study_fk
         join patient p1_0 on p1_0.pk = s2_0.patient_fk
         join patient_id p2_0 on p1_0.pk = p2_0.patient_fk
         join dicomattrs a1_0 on a1_0.pk = p1_0.dicomattrs_fk
         left join study_query_attrs q1_0 on s2_0.pk = q1_0.study_fk and q1_0.view_id=?
         join dicomattrs a2_0 on a2_0.pk = s2_0.dicomattrs_fk
         left join metadata m1_0 on m1_0.pk = s1_0.metadata_fk
         left join series_query_attrs q2_0 on s1_0.pk = q2_0.series_fk and q2_0.view_id=?
         join dicomattrs a3_0 on a3_0.pk = s1_0.dicomattrs_fk
where p2_0.pat_id=?

to avoid performance issue on PostgreSQL caused by https://www.cybertec-postgresql.com/en/subqueries-and-performance-in-postgresql/

PostgreSQL can only execute a scalar subquery as a nested loop

The query will return duplicates, if one patient has multiple matching Patient IDs - which seems quite rare.

@gunterze gunterze added the enhancement New feature or request label Sep 13, 2023
@gunterze gunterze added this to the 5.31.1 milestone Sep 13, 2023
@gunterze gunterze self-assigned this Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant