cockroachdb / cockroachdb/cockroach

sql: not able to generate constrained scan from tuple equality against subquery

Open
#135,745 1 comment 0 reactions 0 assignees View on GitHub
A-sql-optimizer branch-release-24.3 C-performance T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

With a single-column equality against a subquery, CRDB can generate a constrained scan of the primary index (which becomes a lookup join):

```sql
CREATE TABLE abcd (a INT NOT NULL, b INT NOT NULL, c INT NOT NULL, d INT NOT NULL, PRIMARY KEY (a), INDEX (c));
EXPLAIN UPDATE abcd SET d = d + 10 WHERE a = (SELECT a FROM abcd ORDER BY c LIMIT 1);
```

But if we change the PK to a composite key, and the single-column equality to tuple equality, CRDB cannot generate the constrained scan. (We still get the constrained scan if constants are used instead of a subquery, however.)

```sql
CREATE TABLE abcd2 (a INT NOT NULL, b INT NOT NULL, c INT NOT NULL, d INT NOT NULL, PRIMARY KEY (a, b), INDEX (c));
-- full scan of abcd2
EXPLAIN UPDATE abcd2 SET d = d + 10 WHERE (a, b) = (SELECT a, b FROM abcd2 ORDER BY c LIMIT 1);
-- using constants instead of a subquery does produce a constrained scan, however:
EXPLAIN UPDATE abcd2 SET d = d + 10 WHERE (a, b) = (0, 0);
```

This is using `v24.3.0-beta.3`.

Jira issue: CRDB-44696

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.