cockroachdb / cockroachdb/cockroach
opt: optimize join on unnest of multiple constant arrays
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
Consider the schema and query:
```sql
CREATE TABLE t (
r TEXT,
i INT,
INDEX (r, i)
);
EXPLAIN (OPT, VERBOSE)
WITH params AS (
SELECT *
FROM unnest(ARRAY['foo', 'bar']::TEXT[], ARRAY[1, 2]::INT[])
AS tmp(r, i)
)
SELECT *
FROM t JOIN params
ON t.r = params.r AND t.i = params.i;
```
The query plan for the query performs a lookup join for each row produced by `unnest`:
```
inner-join (lookup t@t_r_i_idx)
├── columns: r:3 i:4 r:8 i:9
├── key columns: [8 9] = [3 4]
├── immutable
├── stats: [rows=0.9801, distinct(3)=0.9801, null(3)=0, distinct(4)=0.9801, null(4)=0, distinct(8)=0.9801, null(8)=0, distinct(9)=0.9801, null(9)=0]
├── cost: 206.548011
├── fd: (3)==(8), (8)==(3), (4)==(9), (9)==(4)
├── distribution: us-east1
├── project
│ ├── columns: r:8 i:9
│ ├── immutable
│ ├── stats: [rows=10, distinct(8)=7, null(8)=0, distinct(9)=7, null(9)=0]
│ ├── cost: 0.46
│ ├── distribution: us-east1
│ ├── prune: (8,9)
│ ├── project-set
│ │ ├── columns: unnest:1 unnest:2
│ │ ├── immutable
│ │ ├── stats: [rows=10, distinct(1)=7, null(1)=0.1, distinct(2)=7, null(2)=0.1]
│ │ ├── cost: 0.14
│ │ ├── distribution: us-east1
│ │ ├── values
│ │ │ ├── cardinality: [1 - 1]
│ │ │ ├── stats: [rows=1]
│ │ │ ├── cost: 0.02
│ │ │ ├── key: ()
│ │ │ ├── distribution: us-east1
│ │ │ └── ()
│ │ └── zip
│ │ └── unnest(ARRAY['foo','bar'], ARRAY[1,2]) [immutable]
│ └── projections
│ ├── unnest:1 [as=r:8, outer=(1)]
│ └── unnest:2 [as=i:9, outer=(2)]
└── filters (true)
```
It would be more efficient to perform a constrained scan directly on the index.
I believe this will require expanding the `ConvertZipArraysToValues` rule to work with multi-argument `unnest` invocations to transform the plan into an `(InnerJoin (Scan) (Values))`. It will probably require an additional rule to further turn the inner join into a constrained scan.
Jira issue: CRDB-31583
Contributor guide
Assessment
This issue has not been assessed yet.