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

Migrations to add indexes and realtime support #59

Merged
merged 2 commits into from
Dec 23, 2023

Conversation

menonsamir
Copy link
Collaborator

@menonsamir menonsamir commented Dec 22, 2023

Postgres doesn't automatically index foreign keys, so we add them manually. This speeds up child-to-parent queries significantly, which may help mitigate timeout issues.

I have tested the migrations locally, and confirmed they increase use of indexes (from ~50% on the games table to ~90%). You can compute statistics about index usage on a local database with npx supabase inspect db index-usage --db-url postgresql://postgres:postgres@localhost:54322/postgres.

Also changes README to reflect this.
Copy link

vercel bot commented Dec 22, 2023

Someone is attempting to deploy a commit to a Personal Account owned by @cmnord on Vercel.

@cmnord first needs to authorize it.

Copy link

vercel bot commented Dec 22, 2023

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

Name Status Preview Comments Updated (UTC)
jep ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 22, 2023 11:51pm

@cmnord
Copy link
Owner

cmnord commented Dec 22, 2023

Can you add more context in the commit message (& copy to PR description)? If I look back at the commit later, I'll ask "why did we make this change?". The reason for this change is that:

  • Postgres doesn't automatically index foreign keys
  • Indices will improve the performance of game lookups & joins, which were timing out on Vercel

@cmnord
Copy link
Owner

cmnord commented Dec 22, 2023

Also since we don't have a staging Supabase environment, the only testing is unfortunately local. Can you confirm that the migration works (and maybe improves performance?) locally?

@cmnord
Copy link
Owner

cmnord commented Dec 22, 2023

Another real SWE question would be how long the migration takes to run locally if you pulled down a copy of the full db, but I don't think that's (a) going to be an issue with our DB size and (b) really something I know how to handle if it was too long 😛

@menonsamir menonsamir force-pushed the menonsamir/migrations-index-realtime branch from edb877d to b2cdea3 Compare December 23, 2023 07:19
@menonsamir
Copy link
Collaborator Author

Ok, updated.

- Postgres doesn't automatically index foreign keys,
so we add them manually.
- Indexes speed up child-to-parent queries significantly,
which may help mitigate timeouts when fetching games,
due to the game-category and category-clue joins.
@menonsamir menonsamir force-pushed the menonsamir/migrations-index-realtime branch from b2cdea3 to 6b0dc13 Compare December 23, 2023 07:47
@cmnord cmnord self-requested a review December 23, 2023 07:50
Copy link
Owner

@cmnord cmnord left a comment

Choose a reason for hiding this comment

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

Looks good, thank you for addressing the comments!

I'll have to pull the migration to my laptop and run supabase db push to actually apply the migration, so tests might be wonky for a bit but nothing should break on deployment.

@cmnord cmnord merged commit 4340e00 into cmnord:main Dec 23, 2023
1 of 3 checks passed
@cmnord
Copy link
Owner

cmnord commented Dec 23, 2023

Migration testing results:

Before: running with the prod credentials

$ npx supabase inspect db index-usage


       TABLE NAME     │ PERCENTAGE OF TIMES INDEX USED │ ROWS IN TABLE
  ────────────────────┼────────────────────────────────┼────────────────
    clues             │                              0 │        529930
    categories        │                             97 │        113051
    room_events       │                             85 │         55906
    games             │                             99 │          8713
    rooms             │                             92 │          1565
    ...

Clues at 0% is bad!

$ npx supabase inspect db seq-scans
Connecting to remote database...


          NAME        │  COUNT
  ────────────────────┼──────────
    clues             │ 1244849
    ...
    categories        │  100196
    room_events       │   59210
    games             │   14349
    rooms             │   13211
    ...

Many sequential scans on clues, categories, and room_events!

Testing migration locally

Applied the migration locally and ran again (unfortunately blowing away my local database):

$ npx supabase inspect db index-usage --local


        TABLE NAME     │ PERCENTAGE OF TIMES INDEX USED │ ROWS IN TABLE
  ─────────────────────┼────────────────────────────────┼────────────────
    room_events        │                             91 │            10
    ...
    rooms              │                              8 │             1
    ...
    clues              │                             99 │             0
    ...
    categories         │                             91 │             0
    games              │                              5 │             0
    ...

I upgraded Supabase and this command broke. Not totally sure what's going on here locally. seq-scans also fails. I think this is a Supabase issue. Going to push the migration.

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

Successfully merging this pull request may close these issues.

2 participants