Add namespace-scoped performance indexes on projects#161
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on
query_improvements. Adds the indexes for the two namespace-scoped hot paths, complementing this branch's search / ORDER-BY indexes and N+1 fix. Added as model-levelIndex()to match the convention here.Indexes added (
Projects.__table_args__)ix_projects_namespace_update_dateon(namespace, last_update_date)WHERE namespace = ? ORDER BY last_update_date DESC LIMIT n) → bounded index range scan instead of walking the globallast_update_dateindex and filtering by namespace (the plan flip that scans most of the table for a namespace whose rows are not near the date tip), and no full sort.ix_projects_ns_aggon(namespace) INCLUDE (number_of_samples, name) WHERE private IS FALSESELECT namespace, count(name), sum(number_of_samples) ... GROUP BY namespace) → index-only scan (~25 MB) instead of a ~250 MB heap seq scan. Needs a fresh visibility map, so runVACUUM ANALYZE projectsafter.trgm_index_namespaceGIN onnamespaceILIKEsearch.Why
The two namespace paths were full-scanning the ~250 MB
projectsheap on every request. On a RAM-starved host that means seconds-to-minutes of cold reads. These make both paths read a bounded, cacheable number of pages.Applying to production (existing DB)
create_all(checkfirst) covers fresh installs. For the existing prod table, create concurrently so no blocking lock, then vacuum for the index-only scan:Recommended cleanup while applying (not expressible via
create_all), to cut write amplification:Notes
Index()DDL was verified to compile cleanly against the PostgreSQL dialect.