The code below has been running for 5+ days now to try to setup the drug table, as generated by the DDL script in Atlas. It’s still not finished. Is this run time normal for this query?
I’m running postgresql. Is there any way to optimize the query or optimize Postgresql so that this query runs in a reasonable amount of time (such as adjusting parameters)? Is there another way to get the table so that it doesn’t need to be generated on each setup?
/********** DRUG **********/
INSERT INTO results.concept_hierarchy
(concept_id, concept_name, treemap, level1_concept_name, level2_concept_name, level3_concept_name, level4_concept_name)
SELECT
rxnorm.concept_id,
rxnorm.concept_name AS rxnorm_concept_name,
‘Drug’ AS treemap,
rxnorm.rxnorm_ingredient_concept_name,
atc5_to_atc3.atc5_concept_name,
atc3_to_atc1.atc3_concept_name,
atc1.concept_name AS atc1_concept_name
FROM (
SELECT
c1.concept_id,
c1.concept_name,
c2.concept_id AS rxnorm_ingredient_concept_id,
c2.concept_name AS RxNorm_ingredient_concept_name
FROM vocab.concept c1
INNER JOIN vocab.concept_ancestor ca1 ON c1.concept_id = ca1.descendant_concept_id
AND c1.domain_id = ‘Drug’
INNER JOIN vocab.concept c2 ON ca1.ancestor_concept_id = c2.concept_id
AND c2.domain_id = ‘Drug’
AND c2.concept_class_id = ‘Ingredient’
) rxnorm
LEFT JOIN (
SELECT
c1.concept_id AS rxnorm_ingredient_concept_id,
max(c2.concept_id) AS atc5_concept_id
FROM vocab.concept c1
INNER JOIN vocab.concept_ancestor ca1 ON c1.concept_id = ca1.descendant_concept_id
AND c1.domain_id = ‘Drug’
AND c1.concept_class_id = ‘Ingredient’
INNER JOIN vocab.concept c2 ON ca1.ancestor_concept_id = c2.concept_id
AND c2.vocabulary_id = ‘ATC’
AND c2.concept_class_id = ‘ATC 4th’
GROUP BY c1.concept_id
) rxnorm_to_atc5 ON rxnorm.rxnorm_ingredient_concept_id = rxnorm_to_atc5.rxnorm_ingredient_concept_id
LEFT JOIN (
SELECT
c1.concept_id AS atc5_concept_id,
c1.concept_name AS atc5_concept_name,
max(c2.concept_id) AS atc3_concept_id
FROM vocab.concept c1
INNER JOIN vocab.concept_ancestor ca1 ON c1.concept_id = ca1.descendant_concept_id
AND c1.vocabulary_id = ‘ATC’
AND c1.concept_class_id = ‘ATC 4th’
INNER JOIN vocab.concept c2 ON ca1.ancestor_concept_id = c2.concept_id
AND c2.vocabulary_id = ‘ATC’
AND c2.concept_class_id = ‘ATC 2nd’
GROUP BY c1.concept_id, c1.concept_name
) atc5_to_atc3 ON rxnorm_to_atc5.atc5_concept_id = atc5_to_atc3.atc5_concept_id
LEFT JOIN (
SELECT
c1.concept_id AS atc3_concept_id,
c1.concept_name AS atc3_concept_name,
max(c2.concept_id) AS atc1_concept_id
FROM vocab.concept c1
INNER JOIN vocab.concept_ancestor ca1 ON c1.concept_id = ca1.descendant_concept_id
AND c1.vocabulary_id = ‘ATC’
AND c1.concept_class_id = ‘ATC 2nd’
INNER JOIN vocab.concept c2 ON ca1.ancestor_concept_id = c2.concept_id
AND c2.vocabulary_id = ‘ATC’
AND c2.concept_class_id = ‘ATC 1st’
GROUP BY c1.concept_id, c1.concept_name
) atc3_to_atc1 ON atc5_to_atc3.atc3_concept_id = atc3_to_atc1.atc3_concept_id
LEFT JOIN vocab.concept atc1 ON atc3_to_atc1.atc1_concept_id = atc1.concept_id;