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

Optimize database reindex #4558

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Optimize database reindex #4558

wants to merge 17 commits into from

Conversation

mattwiller
Copy link
Member

@mattwiller mattwiller commented May 15, 2024

In testing on localhost over resource types with 15k – 100k resources in the table, this combination of transaction batching and concurrent handling of the lookup tables reduced the overall full reindex time by 30–50% (saving on the order of several minutes for my small data set).

This PR also moves the reindex logic into an async worker that processes a batch of rows and then enqueues another job to handle the next chunk. This makes the job much more robust, and allows it to more safely run for extended periods of time.

@mattwiller mattwiller added the fhir-datastore Related to the FHIR datastore, includes API and FHIR operations label May 15, 2024
@mattwiller mattwiller added this to the May 31st, 2024 milestone May 15, 2024
@mattwiller mattwiller self-assigned this May 15, 2024
@mattwiller mattwiller requested a review from a team as a code owner May 15, 2024 01:38
Copy link

vercel bot commented May 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
medplum-provider ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 22, 2024 9:37pm
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
medplum-app ⬜️ Ignored (Inspect) Visit Preview May 22, 2024 9:37pm
medplum-storybook ⬜️ Ignored (Inspect) Visit Preview May 22, 2024 9:37pm
medplum-www ⬜️ Ignored (Inspect) Visit Preview May 22, 2024 9:37pm

Copy link

sonarcloud bot commented May 17, 2024

Copy link
Member

@codyebberson codyebberson left a comment

Choose a reason for hiding this comment

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

Overall looks good, nice perf wins 🎉

See questions on forAllResources and transactions


const lastUpdated = resource.meta?.lastUpdated as string;
currentTimestamp = lastUpdated;
await this.withTransaction(async (conn) => {
Copy link
Member

Choose a reason for hiding this comment

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

Is this transaction necessary? Trying to imagine what it's needed for, versus letting the callback handle it.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, I see, you're using this to avoid per-resource transactions in the reindex step.

imho, that kinda breaks the abstraction of forAllResources

  1. I'm not sure the transaction is fully necessary in reindexResource
  2. If it is both necessary and if this is a major performance win, then i think we should probably modify the API contract of forAllResources (maybe a "page" callback and a "resource" callback?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fhir-datastore Related to the FHIR datastore, includes API and FHIR operations
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

None yet

2 participants