Repository navigation
Express image permissions without joins that multiply rows - #1666
Merged
Merged
Conversation
The share branch of the image visibility filter was a LEFT JOIN that multiplied rows, so every caller had to wrap its queryset in distinct() and the first page of a list sorted and deduplicated the full image, accession and cohort rows. The share branch is now a correlated EXISTS, and so is the collections filter of a search, so nothing multiplies rows and the distinct() calls go. The contributor branch stays a join: the accession is one-to-one and its cohort many-to-one, and as a subquery postgres hashes every accession the user owns before the first row.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
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.
The share branch of the image visibility filter was
Q(shares=user), a LEFT JOIN on the share table. It produced one row per share row of the image, for any grantee, so every caller wrapped its queryset indistinct(), and the first page of a list sorted and deduplicated the full image, accession and cohort rows (72 columns) beforeUniquecould collapse them.ExistsonImageShare, and the collections filter of a search anExistsonCollectionImage, so nothing multiplies rows and thedistinct()calls go (image API, search serializer, collection detail page and attribution endpoint, zip download, pin endpoint).ImagePermissions.visible_image_qis the one predicate;build_elasticsearch_querymirrors it as before. The Elasticsearch-backed counts are unchanged.Benchmarks, fastest round, master vs this branch (the
sharesuser has 221k shared images):Known trade-off: inside an OR, Postgres rewrites the share
EXISTSintoid = ANY(hashed subquery)and builds the hash of every share the user has before the first row, LIMIT or not. For a user with 221k shares (production has one with 226k) a DSL-filtered search with no collection is slower than master at 1.1M images:A second, non-hashable correlated clause blocks that rewrite and fixes this one user, but it costs every other sharing user 20 to 60% on filtered pages and 3 to 5x on unbounded scans such as zip listings and counts, so it was left out. For the median production user, with about 1,000 shares, the hashed plan is the fastest of the three.
Tests:
test_image_shares_do_not_multiply_imagesfails on master ([3, 3, 4, 4]for two images) and covers the new invariant; the existing share, collection and search tests cover the rest.