Skip to content

Commit 85162ad

Browse files
committed
optimize clinical event derived table related queries
1 parent bc8ab49 commit 85162ad

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/main/resources/db-scripts/clickhouse/clickhouse.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ CREATE TABLE clinical_event_derived
364364
cancer_study_identifier LowCardinality(String)
365365
)
366366
ENGINE = MergeTree
367-
ORDER BY (event_type, patient_unique_id, cancer_study_identifier);
367+
ORDER BY (cancer_study_identifier, event_type, patient_unique_id);
368368

369369
INSERT INTO clinical_event_derived
370370
SELECT

src/main/resources/mappers/clickhouse/studyview/ClickhouseStudyViewFilterMapper.xml

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@
190190
ced.cancer_study_identifier AS cancer_study_identifier
191191
FROM clinical_event_derived ced
192192
<where>
193+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
194+
cancer_study_identifier IN
195+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
196+
#{studyId}
197+
</foreach>
198+
AND
199+
</if>
193200
key = 'SAMPLE_ID'
194201
AND (event_type ILIKE 'Sample Acquisition' OR event_type ILIKE 'SPECIMEN')
195202
</where>
@@ -202,8 +209,16 @@
202209
value AS treatment,
203210
argMin(start_date, start_date) AS treatment_time_taken
204211
FROM clinical_event_derived
205-
WHERE lower(event_type) = 'treatment'
206-
AND key = 'AGENT'
212+
WHERE
213+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
214+
cancer_study_identifier IN
215+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
216+
#{studyId}
217+
</foreach>
218+
AND
219+
</if>
220+
lower(event_type) = 'treatment'
221+
AND key = 'AGENT'
207222
GROUP BY patient_unique_id, value
208223
) ced_inner ON ced_inner.patient_unique_id = ced.patient_unique_id
209224
<where>
@@ -232,7 +247,14 @@
232247
SELECT patient_unique_id
233248
FROM clinical_event_derived
234249
<where>
235-
<foreach item="dataFilterValue" collection="clinicalEventFilter.values" open="(" separator=") OR (" close=")">
250+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
251+
cancer_study_identifier IN
252+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
253+
#{studyId}
254+
</foreach>
255+
AND
256+
</if>
257+
<foreach item="dataFilterValue" collection="clinicalEventFilter.values" open="((" separator=") OR (" close="))">
236258
event_type = #{dataFilterValue.value}
237259
</foreach>
238260
</where>
@@ -250,9 +272,19 @@
250272
SELECT patient_unique_id
251273
FROM clinical_event_derived
252274
<where>
253-
<foreach item="patientTreatmentFilter" collection="andedPatientTreatmentFilters.getFilters()" open="("
254-
separator=") OR ("
255-
close=")"> lower(event_type) = 'treatment'
275+
<if test="studyViewFilterContext.customDataFilterCancerStudies != null and !studyViewFilterContext.customDataFilterCancerStudies.isEmpty()">
276+
cancer_study_identifier IN
277+
<foreach item="studyId" collection="studyViewFilterContext.customDataFilterCancerStudies" open="(" separator="," close=")">
278+
#{studyId}
279+
</foreach>
280+
AND
281+
</if>
282+
<foreach
283+
item="patientTreatmentFilter"
284+
collection="andedPatientTreatmentFilters.getFilters()"
285+
open="((" separator=") OR (" close="))"
286+
>
287+
lower(event_type) = 'treatment'
256288
AND key = 'AGENT'
257289
AND value = #{patientTreatmentFilter.treatment}
258290
</foreach>

0 commit comments

Comments
 (0)