fix: use NOT EXISTS instead of NOT IN to avoid cross join in pruneCache - #173215
Open
waterWang wants to merge 1 commit into
Open
fix: use NOT EXISTS instead of NOT IN to avoid cross join in pruneCache#173215waterWang wants to merge 1 commit into
waterWang wants to merge 1 commit into
Conversation
|
It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR? Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. Before a member of our team reviews your PR, I have some potential action items for you:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
|
|
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.
Summary
The
pruneCachemethod intable_metadata_updater.godeletes orphaned rows fromsystem.table_metadatawith aNOT IN (SELECT id FROM system.namespace)predicate. Becausesystem.namespace.idis nullable, the optimizer must preserve three-valued logic and lowers the predicate to(table_id = id) IS NOT false. That is not an equality, so it cannot be hash- or lookup-joined and the plan degrades to across join (anti)— costing O(rows(table_metadata) × rows(namespace)). On a 9-node cluster with 100k tables that is ~11.1B comparisons (~38 minutes per run).Fix
Replace the
NOT INsubquery with a correlatedNOT EXISTS, which the optimizer can lower to an equality anti-join:→
This changes the plan from
cross join (anti)(~38 min) tohash join (anti)(~0.2s), verified by the reporter.NOT EXISTSalso eliminates the NULL-hazard ofNOT IN: a single NULL insystem.namespace.idwould otherwise causeNOT INto return the empty set for every input, silently disabling pruning cluster-wide.table_metadata.table_idisNOT NULL, so the result set is identical.Fixes #173143