Skip to content

Commit

Permalink
Updated the nested query to be able to use the index for searching in…
Browse files Browse the repository at this point in the history
…stead of a full table scan (#2335)
  • Loading branch information
aditya-07 committed Nov 22, 2023
1 parent 76ecd8c commit 704d753
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ internal fun Search.getQuery(
filterArgs.addAll(it.args)
}
val whereArgs = mutableListOf<Any>()
val nestedArgs = mutableListOf<Any>()
val query =
when {
isCount -> {
Expand All @@ -311,11 +312,12 @@ internal fun Search.getQuery(
nestedContext != null -> {
whereArgs.add(nestedContext.param.paramName)
val start = "${nestedContext.parentType.name}/".length + 1
nestedArgs.add(nestedContext.parentType.name)
// spotless:off
"""
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceId IN (
WHERE a.resourceType = ? AND a.resourceId IN (
SELECT substr(a.index_value, $start)
FROM ReferenceIndexEntity a
$sortJoinStatement
Expand All @@ -342,7 +344,7 @@ internal fun Search.getQuery(
.split("\n")
.filter { it.isNotBlank() }
.joinToString("\n") { it.trim() }
return SearchQuery(query, sortArgs + type.name + whereArgs + filterArgs + limitArgs)
return SearchQuery(query, nestedArgs + sortArgs + type.name + whereArgs + filterArgs + limitArgs)
}

private val Order?.sqlString: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ class SearchTest {
AND a.resourceUuid IN (
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceId IN (
WHERE a.resourceType = ? AND a.resourceId IN (
SELECT substr(a.index_value, 9)
FROM ReferenceIndexEntity a
WHERE a.resourceType = ? AND a.index_name = ?
Expand All @@ -1844,6 +1844,7 @@ class SearchTest {
assertThat(query.args)
.isEqualTo(
listOf(
ResourceType.Patient.name,
ResourceType.Patient.name,
ResourceType.Condition.name,
Condition.SUBJECT.paramName,
Expand Down Expand Up @@ -1906,7 +1907,7 @@ class SearchTest {
AND a.resourceUuid IN (
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceId IN (
WHERE a.resourceType = ? AND a.resourceId IN (
SELECT substr(a.index_value, 9)
FROM ReferenceIndexEntity a
WHERE a.resourceType = ? AND a.index_name = ?
Expand All @@ -1931,6 +1932,7 @@ class SearchTest {
ResourceType.Patient.name,
Patient.ADDRESS_COUNTRY.paramName,
"IN",
ResourceType.Patient.name,
ResourceType.Immunization.name,
Immunization.PATIENT.paramName,
ResourceType.Immunization.name,
Expand Down Expand Up @@ -1974,7 +1976,7 @@ class SearchTest {
AND a.resourceUuid IN (
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceId IN (
WHERE a.resourceType = ? AND a.resourceId IN (
SELECT substr(a.index_value, 9)
FROM ReferenceIndexEntity a
WHERE a.resourceType = ? AND a.index_name = ?
Expand All @@ -1986,7 +1988,7 @@ class SearchTest {
) AND a.resourceUuid IN(
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceId IN (
WHERE a.resourceType = ? AND a.resourceId IN (
SELECT substr(a.index_value, 9)
FROM ReferenceIndexEntity a
WHERE a.resourceType = ? AND a.index_name = ?
Expand All @@ -2003,13 +2005,15 @@ class SearchTest {
assertThat(query.args)
.isEqualTo(
listOf(
ResourceType.Patient.name,
ResourceType.Patient.name,
ResourceType.Condition.name,
Condition.SUBJECT.paramName,
ResourceType.Condition.name,
Condition.CODE.paramName,
"44054006",
"http:https://snomed.info/sct",
ResourceType.Patient.name,
ResourceType.Condition.name,
Condition.SUBJECT.paramName,
ResourceType.Condition.name,
Expand Down

0 comments on commit 704d753

Please sign in to comment.