sqlstore: replace LIKE queries with range queries for index-friendly#1091
Open
tovmeod wants to merge 1 commit intotulir:mainfrom
Open
sqlstore: replace LIKE queries with range queries for index-friendly#1091tovmeod wants to merge 1 commit intotulir:mainfrom
tovmeod wants to merge 1 commit intotulir:mainfrom
Conversation
…PN-to-LID migration
The MigratePNToLID and deleteAll* functions used LIKE patterns (e.g.
`their_id LIKE $2 || ':%'`) which PostgreSQL cannot optimize with btree
indexes when the pattern is parameterized. This caused sequential scans
on large tables (sender_keys 1.7M rows/2GB, sessions 248K rows/2.4GB)
on every new sender during message decryption.
Replace LIKE with equivalent range queries using ASCII ordering:
`their_id >= $2 || ':' AND their_id < $2 || ';'`
(':' is ASCII 58, ';' is ASCII 59, so this matches exactly the same
rows as LIKE 'phone:%')
This allows PostgreSQL to use the existing btree primary key indexes
for efficient range scans instead of sequential table scans.
Also fix DeleteIdentity to use deleteIdentityQuery (exact match)
instead of deleteAllIdentitiesQuery (was doing a LIKE match on a
single address, which happened to work but was semantically wrong).
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.
sqlstore: replace LIKE queries with range queries for index-friendly PN-to-LID migration
The MigratePNToLID and deleteAll* functions used LIKE patterns (e.g.
their_id LIKE $2 || ':%') which PostgreSQL cannot optimize with btree indexes when the pattern is parameterized. This caused sequential scans on large tables (sender_keys 1.7M rows/2GB, sessions 248K rows/2.4GB) on every new sender during message decryption.Replace LIKE with equivalent range queries using ASCII ordering:
their_id >= $2 || ':' AND their_id < $2 || ';'(':' is ASCII 58, ';' is ASCII 59, so this matches exactly the same rows as LIKE 'phone:%')
This allows PostgreSQL to use the existing btree primary key indexes for efficient range scans instead of sequential table scans.
Also fix DeleteIdentity to use deleteIdentityQuery (exact match) instead of deleteAllIdentitiesQuery (was doing a LIKE match on a single address, which happened to work but was semantically wrong).