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

Fix Date/DateTime indexes to speed up Left joins on Search #2047

Closed
wants to merge 7 commits into from
Closed

Fix Date/DateTime indexes to speed up Left joins on Search #2047

wants to merge 7 commits into from

Conversation

omarismail94
Copy link
Contributor

@omarismail94 omarismail94 commented Jun 26, 2023

IMPORTANT: All PRs must be linked to an issue (except for extremely trivial and straightforward changes).

Fixes #2040

Description
Clear and concise code change description.

In the Search API, we perform a LEFT JOIN when sorting:

LEFT JOIN ${sortTableName.tableName} $tableAlias
      ON a.resourceType = $tableAlias.resourceType AND a.resourceUuid = $tableAlias.resourceUuid AND $tableAlias.index_name = ?

this join uses the resourceType, resourceUuid and index_name columns. To speed this up, we should create a composite index made up of these 3 columns

TESTED:
Downloaded 7000 households. Opened App Inspector and ran query generated. Returned results almost instantaneously as opposed to many seconds

Alternative(s) considered
Have you considered any alternatives? And if so, why have you chosen the approach in this PR?

Type
Choose one: (Bug fix | Feature | Documentation | Testing | Code health | Builds | Releases | Other)

Checklist

  • I have read and acknowledged the Code of conduct.
  • I have read the Contributing page.
  • I have signed the Google Individual CLA, or I am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have run ./gradlew spotlessApply and ./gradlew spotlessCheck to check my code follows the style guide of this project.
  • I have run ./gradlew check and ./gradlew connectedCheck to test my changes locally.
  • I have built and run the demo app(s) to verify my change fixes the issue and/or does not break the demo app(s).

@omarismail94 omarismail94 requested review from a team and santosh-pingle as code owners June 26, 2023 12:44
ndegwamartin added a commit to opensrp/android-fhir that referenced this pull request Jun 26, 2023
   - With unmerged PR #1
   - With unmerged PR google#1917
   - With unmerged PR google#1978
   - With unmerged PR google#1907
   - With unmerged PR google#2032
   - With unmerged PR google#1669
   - With unmerged PR google#2047
@@ -70,7 +70,22 @@ val MIGRATION_2_3 =
object : Migration(/* startVersion = */ 2, /* endVersion = */ 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"CREATE INDEX IF NOT EXISTS `index_DateTimeIndexEntity_index_from` ON `DateTimeIndexEntity` (`index_from`)"
"CREATE INDEX IF NOT EXISTS `index_DateTimeIndexEntity_resourceType_index_name_resourceUuid_index_from_index_to` ON `DateTimeIndexEntity` (`resourceType`, `index_name`, `resourceUuid`, `index_from`, `index_to`)"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this needs to change.

the way migration works is that, if you're on db version n, the migration code would be run to migrate your db schema from n to n+1.

this means you should NOT modify existing migration code - people already on v3 would not get your new db index!

you should create a new version, and write another migration step to handle 3 to 4.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aditya-07 fyi since aditya added migration code to begin with.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jingtang10 DB version in the latest released engine library is 2. DB version 3 was added for a new change and hasn't been released via the library.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure - but as far as i know @khyativyasargus, @nsabale7, @dubdabasoduba fork our libs... I know theoretically we're not making promises in between releases, but i don't really see the downside of creating another migration step.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted to how I have it but for some reason GitHub hated me doing that on this PR and the change is not getting picked up here.

Created a new PR: #2063 that has the migration to a new version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Search method takes more time when load 7k household list
3 participants