cockroachdb / cockroachdb/cockroach

sql: push down subset of filters into unions

Open
#102,983 0 comments 0 reactions 0 assignees View on GitHub
C-wishlist O-support P-3 T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

We should be able to push a subset of filters into inlined CTEs. In the following example, the predicates `a='foo'` and `c='bar'` could be pushed down into the union to avoid a full scan of `t`, but are not. This can cause performance problems if `t` is a large table.

```
CREATE TABLE t(
a STRING,
b STRING,
c STRING,
d STRING,
PRIMARY KEY (c),
INDEX idx1 (a, c)
);

CREATE TABLE s (
a STRING,
b STRING
);

EXPLAIN WITH
cte AS (SELECT * FROM t UNION ALL SELECT * FROM t)
SELECT
c,
d,
b
FROM cte
WHERE a = 'foo'
AND c = 'bar'
AND (d = 'baz' OR b = ANY (SELECT DISTINCT b FROM s
));
```

Plan on 23.1:
```
• root

├── • filter
│ │ estimated row count: 1
│ │ filter: ((a = 'foo') AND (c = 'bar')) AND ((d = 'baz') OR (b = ANY @S1))
│ │
│ └── • render
│ │
│ └── • union all
│ │ estimated row count: 2
│ │
│ ├── • scan
│ │ estimated row count: 1 (100% of the table; stats collected 2 minutes ago)
│ │ table: t@t_pkey
│ │ spans: FULL SCAN
│ │
│ └── • scan
│ estimated row count: 1 (100% of the table; stats collected 2 minutes ago)
│ table: t@t_pkey
│ spans: FULL SCAN

└── • subquery
│ id: @S1
│ original sql: (SELECT DISTINCT b FROM s)
│ exec mode: any rows

└── • distinct
│ estimated row count: 1
│ distinct on: b

└── • scan
estimated row count: 1 (100% of the table; stats collected 2 minutes ago)
table: s@s_pkey
spans: FULL SCAN
```

Jira issue: CRDB-27769

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.