cockroachdb / cockroachdb/cockroach
sql: generic query plans obscure query distribution
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
When selecting a query plan, we add a cost penalty to plans that may touch nodes outside of the gateway region. This is a problem for generic query plans, since it may not be possible to determine while preparing a query whether it might reach outside the gateway region. This can make custom query plans more attractive, even despite the planning overhead they add. Here's an example:
```
./cockroach demo --global --empty --nodes=9
CREATE DATABASE testdb PRIMARY REGION "us-west1" REGIONS "europe-west1", "us-east1";
USE testdb;
CREATE TABLE t (x INT PRIMARY KEY, y INT) LOCALITY REGIONAL BY ROW;
INSERT INTO t VALUES (1, 2) RETURNING crdb_region;
PREPARE p AS SELECT *, x + y FROM t WHERE x = $1 AND crdb_region = $2;
EXPLAIN ANALYZE (DEBUG) EXECUTE p(1, 'us-east1');
SET plan_cache_mode = force_generic_plan;
EXPLAIN ANALYZE (DEBUG) EXECUTE p(1, 'us-east1');
```
Custom plan:
```
project
├── columns: x:1!null y:2 "?column?":8
├── cardinality: [0 - 1]
├── immutable
├── stats: [rows=1]
├── cost: 5.16
├── key: ()
├── fd: ()-->(1,2,8)
├── distribution: us-east1
├── prune: (1,2,8)
├── scan t
│ ├── columns: x:1!null y:2 crdb_region:3!null
│ ├── constraint: /3/1: [/'us-east1'/1 - /'us-east1'/1]
│ ├── cardinality: [0 - 1]
│ ├── immutable
│ ├── stats: [rows=1, distinct(1)=1, null(1)=0, distinct(3)=1, null(3)=0]
│ ├── cost: 5.13
│ ├── key: ()
│ ├── fd: ()-->(1-3)
│ └── distribution: us-east1
└── projections
└── x:1 + y:2 [as="?column?":8, outer=(1,2), immutable]
```
Generic plan:
```
project
├── columns: x:1!null y:2 "?column?":8
├── cardinality: [0 - 1]
├── immutable, has-placeholder
├── stats: [rows=1]
├── cost: 204.79
├── key: ()
├── fd: ()-->(1,2,8)
├── distribution: us-east1
├── prune: (1,2,8)
├── project
│ ├── columns: x:1!null y:2 crdb_region:3!null
│ ├── cardinality: [0 - 1]
│ ├── immutable, has-placeholder
│ ├── stats: [rows=1]
│ ├── cost: 204.76
│ ├── key: ()
│ ├── fd: ()-->(1-3)
│ ├── distribution: us-east1
│ └── inner-join (lookup t)
│ ├── columns: x:1!null y:2 crdb_region:3!null "$1":9!null "$2":10!null
│ ├── flags: disallow merge join
│ ├── key columns: [10 9] = [3 1]
│ ├── lookup columns are key
│ ├── cardinality: [0 - 1]
│ ├── immutable, has-placeholder
│ ├── stats: [rows=0.333333]
│ ├── cost: 204.74
│ ├── key: ()
│ ├── fd: ()-->(1-3,9,10), (1)==(9), (9)==(1), (3)==(10), (10)==(3)
│ ├── distribution: us-east1
│ ├── values
│ │ ├── columns: "$1":9 "$2":10
│ │ ├── cardinality: [1 - 1]
│ │ ├── has-placeholder
│ │ ├── stats: [rows=1]
│ │ ├── cost: 0.02
│ │ ├── key: ()
│ │ ├── fd: ()-->(9,10)
│ │ ├── distribution: us-east1
│ │ └── ($1, $2)
│ └── filters (true)
└── projections
└── x:1 + y:2 [as="?column?":8, outer=(1,2), immutable]
```
Jira issue: CRDB-51256
Contributor guide
Assessment
This issue has not been assessed yet.