Skip to content
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

opt: support = like IN for locality optimized search #93141

Open
ajwerner opened this issue Dec 6, 2022 · 0 comments
Open

opt: support = like IN for locality optimized search #93141

ajwerner opened this issue Dec 6, 2022 · 0 comments
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-quick-win Likely to be a quick win for someone experienced. T-sql-queries SQL Queries Team
Projects

Comments

@ajwerner
Copy link
Contributor

ajwerner commented Dec 6, 2022

Is your feature request related to a problem? Please describe.
I'm sure that I'm not using the right terminology here. I was following along with #91084 and I tried it out myself (without copy-paste) and I typed something marginally different from the query was shown as working. I believe that it's the same, on some level, but it behaves very differently.

Consider:

SET experimental_enable_unique_without_index_constraints = true;

CREATE TABLE accounts (
    account_id  UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name        STRING NOT NULL,
    crdb_region crdb_internal_region NOT NULL,
    UNIQUE WITHOUT INDEX (account_id, crdb_region)
) LOCALITY GLOBAL;

CREATE TABLE tweets (
    account_id UUID NOT NULL,
    tweet_id   UUID DEFAULT gen_random_uuid(),
    message    STRING NOT NULL,
    PRIMARY KEY (account_id, tweet_id),
    FOREIGN KEY (account_id, crdb_region) REFERENCES accounts (account_id, crdb_region) ON DELETE CASCADE ON UPDATE CASCADE
) LOCALITY REGIONAL BY ROW;

The example query which works today is:

EXPLAIN
SELECT tweet_id, message
FROM tweets
WHERE (crdb_region, account_id) IN
  (SELECT crdb_region, account_id
   FROM accounts
   WHERE account_id = '6f781502-4936-43cc-b384-04e5cf292cc8');

This works nicely. Now, instead, consider:

EXPLAIN
SELECT tweet_id, message
FROM tweets
WHERE (crdb_region, account_id) =
  (SELECT crdb_region, account_id
   FROM accounts
   WHERE account_id = '6f781502-4936-43cc-b384-04e5cf292cc8');

The differences is = instead of IN. I would expect that this can be equally optimized the same way, but it's not.

Jira issue: CRDB-22186

@ajwerner ajwerner added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label Dec 6, 2022
@ajwerner ajwerner added this to Triage in SQL Queries via automation Dec 6, 2022
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Dec 6, 2022
@msirek msirek moved this from Triage to Backlog in SQL Queries Dec 6, 2022
@rytaft rytaft added the E-quick-win Likely to be a quick win for someone experienced. label Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) E-quick-win Likely to be a quick win for someone experienced. T-sql-queries SQL Queries Team
Projects
Status: Backlog
SQL Queries
Backlog (DO NOT ADD NEW ISSUES)
Development

No branches or pull requests

2 participants