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

Bug: SQL cannot be executed in a reasonable time #46

Open
kapsner opened this issue May 17, 2022 · 2 comments
Open

Bug: SQL cannot be executed in a reasonable time #46

kapsner opened this issue May 17, 2022 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@kapsner
Copy link
Member

kapsner commented May 17, 2022

This SQL statement takes very very long to return the results:

SELECT 
r1.fhir_id AS "Fall.Versorgungsstellenkontakt.Aufnahmenummer", 
r2.cd_system ->> 'code' AS "Laborbefund.Laboruntersuchung.Code" 
FROM ( SELECT * FROM ( 
SELECT 
fhir_id, 
to_timestamp(jsonb_path_query(DATA, '$.period') ->> 'start', 'YYYY-MM-DDTHH:MI:SS') AS fhir_start_date, 
jsonb_array_elements_text(jsonb_path_query(DATA, '$.meta.profile')) AS fhir_profile 
FROM resources 
WHERE TYPE = 'Encounter') AS r_intermediate ) r1 
LEFT JOIN ( 
SELECT 
REPLACE(jsonbdata2 -> 'encounter' ->> 'reference', 'Encounter/', '') AS eid, 
cd_system FROM ( 
SELECT 
DATA AS jsonbdata2, 
jsonb_array_elements(jsonb_path_query(DATA, '$.code.coding')) AS cd_system, 
jsonb_array_elements(jsonb_path_query(DATA, '$.category.coding')) AS cd_category 
FROM resources 
WHERE TYPE = 'Observation') r3 
WHERE r3.cd_system ->> 'system' = 'http://loinc.org/' AND 
r3.cd_category ->> 'code' = '26436-6' 
) r2 ON 
r2.eid = r1.fhir_id 
WHERE r1.fhir_profile = 'https://fhir.miracum.org/core/StructureDefinition/Versorgungsfall';

It would be great, if the fhir-gateway could somehow be "enhanced" in a way, that this and similar SQL statements can be executed in a reasonable amount of time.

Some suggestions exist already, e.g.

@joundso joundso added bug Something isn't working enhancement New feature or request labels May 17, 2022
@rajesh-murali
Copy link

rajesh-murali commented Apr 21, 2023

Would it be ok if we add a filter on date to systematically filter and search. Similar to the following where an effectivedate. However the Query need to be tested

SELECT 
r1.fhir_id AS "Fall.Versorgungsstellenkontakt.Aufnahmenummer", 
r2.cd_system ->> 'code' AS "Laborbefund.Laboruntersuchung.Code" 
FROM ( SELECT * FROM ( 
SELECT 
fhir_id, 
to_timestamp(jsonb_path_query(DATA, '$.period') ->> 'start', 'YYYY-MM-DDTHH:MI:SS') AS fhir_start_date, 
jsonb_array_elements_text(jsonb_path_query(DATA, '$.meta.profile')) AS fhir_profile 
FROM resources 
WHERE TYPE = 'Encounter') AS r_intermediate ) r1 
LEFT JOIN ( 
SELECT 
REPLACE(jsonbdata2 -> 'encounter' ->> 'reference', 'Encounter/', '') AS eid, 
cd_system FROM ( 
SELECT 
DATA AS jsonbdata2, 
jsonb_array_elements(jsonb_path_query(DATA, '$.code.coding')) AS cd_system, 
jsonb_array_elements(jsonb_path_query(DATA, '$.category.coding')) AS cd_category, 
jsonb_array_elements(jsonb_path_query(DATA, '$.effectiveDatetime')) AS cd_effectivedate
FROM resources 
WHERE TYPE = 'Observation') r3 
WHERE r3.cd_system ->> 'system' = 'http://loinc.org/' AND 
r3.cd_category ->> 'code' = '26436-6' AND
r3.cd_effectivedate ->> 'date' BETWEEN TO_DATE('2020-01-01T00:00:00','YYYY-MM-DD"T"HH24:MI:SS') AND TO_DATE('2020-01-02T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS')
) r2 ON 
r2.eid = r1.fhir_id 
WHERE r1.fhir_profile = 'https://fhir.miracum.org/core/StructureDefinition/Versorgungsfall';

@kapsner
Copy link
Member Author

kapsner commented Apr 21, 2023

@rajesh-murali thanks for your suggestion. We do that already by replacing AS r_intermediate with AS r_intermediate WHERE r_intermediate.fhir_start_date BETWEEN .... AND. ....

However, that doesn't help as well.

FYI @KoesterH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants