opt: add implicit join with global table using FK constraint to avoid scanning remote rows #91084
Labels
C-enhancement
Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
O-support
Would prevent or help troubleshoot a customer escalation - bugs, missing observability/tooling, docs
P-3
Issues/test failures with no fix SLA
T-sql-queries
SQL Queries Team
This issue is originally described towards the end of #69617.
Describe the solution you'd like
Given a query on a REGIONAL BY ROW table that has a foreign key constraint on a global table, when the query contains a predicate on the key of the global table and the crdb_region column value is not specified via an equality filter, add an implicit join with the global table to potentially avoid scanning rows in remote regions.
Example:
This query in its current form must access all regions because the primary key of
tweets
is not fully specified, so locality-optimized search cannot be used. The global table can be accessed locally, and specifies thecrdb_region
column value of the row intweets
via the FK constraint, so transforming the query into the following avoids the unconditional scan from all regions:It's still possible the
crdb_region
column value is a remote region, but this plan at least provides the option of skipping scans from remote regions whencrdb_region
is the local region, or if no row with the specifiedaccount_id
exists inaccounts
.A second issue is that
UNIQUE WITHOUT INDEX (account_id, crdb_region)
should not be required to create the foreign key constraint as the primary key column,account_id
, implies the uniqueness of(account_id, crdb_region)
. This issue could optionally do away with this unnecessary requirement.Describe alternatives you've considered
None
Jira issue: CRDB-21100
The text was updated successfully, but these errors were encountered: