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

Updated the nested query to be able to use the index for searching instead of a full table scan #2335

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Updated the nested query to be able to use the index for searching in…
…stead of a full table scan
  • Loading branch information
aditya-07 committed Nov 22, 2023
commit 2edd725cf6fc56a837489642cd307fdfb349f4d2
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
Loading