-
Notifications
You must be signed in to change notification settings - Fork 194
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
feat(sync-service): index for array contains #2359
Merged
Merged
+752
−355
Conversation
This file contains 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
707acda
to
11fa653
Compare
11fa653
to
643c266
Compare
643c266
to
10c5854
Compare
alco
approved these changes
Feb 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job!
packages/sync-service/lib/electric/shapes/filter/where_condition.ex
Outdated
Show resolved
Hide resolved
packages/sync-service/lib/electric/shapes/filter/indexes/equality_index.ex
Show resolved
Hide resolved
packages/sync-service/lib/electric/shapes/filter/indexes/inclusion_index.ex
Outdated
Show resolved
Hide resolved
icehaunter
approved these changes
Feb 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good job! 🔥 My only nitpick would be nested update formatting, but I've relayed that to you in DMs, feel free to address or not
765c282
to
bef3973
Compare
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.
This PR seeks to address trigger.dev's WAL lag issues by adding two optimisations to where clause filtering:
Allow multiple conditions in a where clause to be optimised (not just one)
This feature alone should halve the processing time for trigger.dev as they have many where clauses in the form
WHERE runtimeEnvironmentId = ? AND batchId = ?
where if a change matched theruntimeEnvironmentId
it would then have to iterate through thebatchId
s. Now it doesn't - thebatchId
condition is indexed as well.Optimise where clauses that have a condition in the form 'array_field @> array_const'
Trigger.dev's other problematic where clauses are in the form
WHERE runtimeEnvironmentId = ? AND runTags @> ?
and this feature optimises the@>
operation. The algorithm for this optimisation can be seen in the InclusionIndex module.Comparing the performance of this index against the current method shows quite a dramatic difference:

Here you can see it performs well with 100k shapes (which for trigger.dev would be 100k shapes with the same

runtimeEnvironmentId
, in other words 100k shapes per user)Both of the charts above are based on a shape array size of 3 and a change array size of 10 which is typical for trigger.dev.
The algorithm seems to scale roughly linearly with change array size:

The algorithm seems to scale well with shape array size for a fixed change array size (10):
