From d5593d5dd056b8ddd002bca9973a644cc79e43a1 Mon Sep 17 00:00:00 2001 From: pieterlukasse Date: Tue, 5 Mar 2024 17:11:05 +0100 Subject: [PATCH] wip: trying out different query to test for perf improvements --- models/cohortdata.go | 3 +-- models/helper.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/models/cohortdata.go b/models/cohortdata.go index 7a5f1bc..382c0d9 100644 --- a/models/cohortdata.go +++ b/models/cohortdata.go @@ -124,8 +124,7 @@ func (h CohortData) RetrieveCohortOverlapStatsWithoutFilteringOnConceptValue(sou Joins("INNER JOIN " + resultsDataSource.Schema + ".cohort as control_cohort ON control_cohort.subject_id = case_cohort_unionedAndIntersectedWithFilters.subject_id") // this one allows for the intersection between case and control and the assessment of the overlap if len(otherFilterConceptIds) > 0 { - query = query.Joins("INNER JOIN " + omopDataSource.Schema + ".observation_continuous as observation" + omopDataSource.GetViewDirective() + " ON control_cohort.subject_id = observation.person_id") - query = QueryFilterByConceptIdsHelper(query, sourceId, otherFilterConceptIds, omopDataSource, resultsDataSource.Schema, "observation") + query = QueryFilterByConceptIdsHelper2(query, sourceId, otherFilterConceptIds, omopDataSource, resultsDataSource.Schema, "control_cohort.subject_id") } query = query.Where("control_cohort.cohort_definition_id = ?", controlCohortId) query, cancel := utils.AddTimeoutToQuery(query) diff --git a/models/helper.go b/models/helper.go index fd02fee..1040c8f 100644 --- a/models/helper.go +++ b/models/helper.go @@ -25,6 +25,21 @@ func QueryFilterByConceptIdsHelper(query *gorm.DB, sourceId int, filterConceptId return query } +// +func QueryFilterByConceptIdsHelper2(query *gorm.DB, sourceId int, filterConceptIds []int64, + omopDataSource *utils.DbAndSchema, resultSchemaName string, personIdFieldForObservationJoin string) *gorm.DB { + // iterate over the filterConceptIds, adding a new INNER JOIN and filters for each, so that the resulting set is the + // set of persons that have a non-null value for each and every one of the concepts: + for i, filterConceptId := range filterConceptIds { + observationTableAlias := fmt.Sprintf("observation_filter_%d", i) + log.Printf("Adding extra INNER JOIN with alias %s", observationTableAlias) + query = query.Joins("INNER JOIN "+omopDataSource.Schema+".observation_continuous as "+observationTableAlias+omopDataSource.GetViewDirective()+" ON "+observationTableAlias+".person_id = "+personIdFieldForObservationJoin). + Where(observationTableAlias+".observation_concept_id = ?", filterConceptId). + Where(GetConceptValueNotNullCheckBasedOnConceptType(observationTableAlias, sourceId, filterConceptId)) + } + return query +} + // Helper function that adds extra filter clauses to the query, for the given filterCohortPairs, intersecting on the // right set of tables, excluding data where necessary, etc. // It basically iterates over the list of filterCohortPairs, adding relevant INTERSECT and EXCEPT clauses, so that the resulting set is the