Skip to content

Commit

Permalink
opt: fetch minimal set of columns on returning mutations
Browse files Browse the repository at this point in the history
Previously, we used to fetch all columns when a mutation contained
a `RETURNING` clause. This is an issue because it forces us to
retrieve unnecessary data and creates extra contention.
This change adds logic to compute the minimal set of required columns
and fetches only those.

Fixes cockroachdb#30618.
Unblocks cockroachdb#30624.

Release note: None
  • Loading branch information
Ridwan Sharif committed Jul 1, 2019
1 parent 701719b commit 6fd7498
Show file tree
Hide file tree
Showing 18 changed files with 715 additions and 441 deletions.
2 changes: 1 addition & 1 deletion c-deps/libedit
Submodule libedit updated 1 files
+9 −4 src/terminal.c
20 changes: 9 additions & 11 deletions pkg/sql/opt/exec/execbuilder/testdata/delete
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,12 @@ count · ·
query TTT
EXPLAIN DELETE FROM indexed WHERE value = 5 LIMIT 10 RETURNING id
----
render · ·
└── run · ·
└── delete · ·
│ from indexed
│ strategy deleter
└── index-join · ·
│ table indexed@primary
└── scan · ·
· table indexed@indexed_value_idx
· spans /5-/6
· limit 10
render · ·
└── run · ·
└── delete · ·
│ from indexed
│ strategy deleter
└── scan · ·
· table indexed@indexed_value_idx
· spans /5-/6
· limit 10
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/orderby
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ render · · (b) ·
└── delete · · (a, b, c) ·
│ from t · ·
│ strategy deleter · ·
└── scan · · (a, b, c) ·
└── scan · · (a, b) ·
· table t@primary · ·
· spans /3-/3/# · ·

Expand Down
87 changes: 47 additions & 40 deletions pkg/sql/opt/memo/testdata/logprops/insert
Original file line number Diff line number Diff line change
Expand Up @@ -198,53 +198,60 @@ project
build
INSERT INTO abcde (a, b) (VALUES (1, 2)) RETURNING *, rowid;
----
insert abcde
project
├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(int) rowid:5(int!null)
├── insert-mapping:
│ ├── column1:7 => a:1
│ ├── column2:8 => b:2
│ ├── column9:9 => c:3
│ ├── column12:12 => d:4
│ ├── column10:10 => rowid:5
│ └── column11:11 => e:6
├── cardinality: [1 - 1]
├── side-effects, mutations
├── key: ()
├── fd: ()-->(1-5)
└── project
├── columns: column12:12(int) column1:7(int!null) column2:8(int!null) column9:9(int!null) column10:10(int) column11:11(int)
├── prune: (1-5)
└── insert abcde
├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(int) rowid:5(int!null)
├── insert-mapping:
│ ├── column1:7 => a:1
│ ├── column2:8 => b:2
│ ├── column9:9 => c:3
│ ├── column12:12 => d:4
│ ├── column10:10 => rowid:5
│ └── column11:11 => e:6
├── cardinality: [1 - 1]
├── side-effects
├── side-effects, mutations
├── key: ()
├── fd: ()-->(7-12)
├── prune: (7-12)
├── project
│ ├── columns: column9:9(int!null) column10:10(int) column11:11(int) column1:7(int!null) column2:8(int!null)
│ ├── cardinality: [1 - 1]
│ ├── side-effects
│ ├── key: ()
│ ├── fd: ()-->(7-11)
│ ├── prune: (7-11)
│ ├── values
│ │ ├── columns: column1:7(int!null) column2:8(int!null)
│ │ ├── cardinality: [1 - 1]
│ │ ├── key: ()
│ │ ├── fd: ()-->(7,8)
│ │ ├── prune: (7,8)
│ │ └── tuple [type=tuple{int, int}]
│ │ ├── const: 1 [type=int]
│ │ └── const: 2 [type=int]
│ └── projections
│ ├── const: 10 [type=int]
│ ├── function: unique_rowid [type=int, side-effects]
│ └── cast: INT8 [type=int]
│ └── null [type=unknown]
└── projections
└── plus [type=int, outer=(8,9)]
├── plus [type=int]
│ ├── variable: column2 [type=int]
│ └── variable: column9 [type=int]
└── const: 1 [type=int]
├── fd: ()-->(1-5)
└── project
├── columns: column12:12(int) column1:7(int!null) column2:8(int!null) column9:9(int!null) column10:10(int) column11:11(int)
├── cardinality: [1 - 1]
├── side-effects
├── key: ()
├── fd: ()-->(7-12)
├── prune: (7-12)
├── project
│ ├── columns: column9:9(int!null) column10:10(int) column11:11(int) column1:7(int!null) column2:8(int!null)
│ ├── cardinality: [1 - 1]
│ ├── side-effects
│ ├── key: ()
│ ├── fd: ()-->(7-11)
│ ├── prune: (7-11)
│ ├── values
│ │ ├── columns: column1:7(int!null) column2:8(int!null)
│ │ ├── cardinality: [1 - 1]
│ │ ├── key: ()
│ │ ├── fd: ()-->(7,8)
│ │ ├── prune: (7,8)
│ │ └── tuple [type=tuple{int, int}]
│ │ ├── const: 1 [type=int]
│ │ └── const: 2 [type=int]
│ └── projections
│ ├── const: 10 [type=int]
│ ├── function: unique_rowid [type=int, side-effects]
│ └── cast: INT8 [type=int]
│ └── null [type=unknown]
└── projections
└── plus [type=int, outer=(8,9)]
├── plus [type=int]
│ ├── variable: column2 [type=int]
│ └── variable: column9 [type=int]
└── const: 1 [type=int]

# Filter FD set.
build
Expand Down
67 changes: 40 additions & 27 deletions pkg/sql/opt/memo/testdata/stats/delete
Original file line number Diff line number Diff line change
Expand Up @@ -44,50 +44,63 @@ select
├── stats: [rows=3.3, distinct(1)=3.3, null(1)=0, distinct(2)=3.27803262, null(2)=0, distinct(3)=0.981940882, null(3)=0]
├── key: (1)
├── fd: ()-->(3), (1)-->(2)
├── delete xyz
├── project
│ ├── columns: x:1(string!null) y:2(int!null) z:3(float!null)
│ ├── fetch columns: x:4(string) y:5(int) z:6(float)
│ ├── side-effects, mutations
│ ├── stats: [rows=9.9, distinct(1)=9.9, null(1)=0, distinct(2)=9.5617925, null(2)=0, distinct(3)=1, null(3)=0]
│ ├── key: (1)
│ ├── fd: ()-->(3), (1)-->(2)
│ └── select
│ ├── columns: x:4(string!null) y:5(int!null) z:6(float!null)
│ ├── stats: [rows=9.9, distinct(4)=9.9, null(4)=0, distinct(5)=9.5617925, null(5)=0, distinct(6)=1, null(6)=0]
│ ├── key: (4)
│ ├── fd: ()-->(6), (4)-->(5)
│ ├── scan xyz
│ │ ├── columns: x:4(string!null) y:5(int!null) z:6(float)
│ │ ├── stats: [rows=1000, distinct(4)=1000, null(4)=0, distinct(5)=100, null(5)=0, distinct(6)=100, null(6)=10]
│ │ ├── key: (4)
│ │ └── fd: (4)-->(5,6)
│ └── filters
│ └── z = 5.5 [type=bool, outer=(6), constraints=(/6: [/5.5 - /5.5]; tight), fd=()-->(6)]
│ └── delete xyz
│ ├── columns: x:1(string!null) y:2(int!null) z:3(float!null)
│ ├── fetch columns: x:4(string) y:5(int) z:6(float)
│ ├── side-effects, mutations
│ ├── stats: [rows=9.9, distinct(1)=9.9, null(1)=0, distinct(2)=9.5617925, null(2)=0, distinct(3)=1, null(3)=0]
│ ├── key: (1)
│ ├── fd: ()-->(3), (1)-->(2)
│ └── select
│ ├── columns: x:4(string!null) y:5(int!null) z:6(float!null)
│ ├── stats: [rows=9.9, distinct(4)=9.9, null(4)=0, distinct(5)=9.5617925, null(5)=0, distinct(6)=1, null(6)=0]
│ ├── key: (4)
│ ├── fd: ()-->(6), (4)-->(5)
│ ├── scan xyz
│ │ ├── columns: x:4(string!null) y:5(int!null) z:6(float)
│ │ ├── stats: [rows=1000, distinct(4)=1000, null(4)=0, distinct(5)=100, null(5)=0, distinct(6)=100, null(6)=10]
│ │ ├── key: (4)
│ │ └── fd: (4)-->(5,6)
│ └── filters
│ └── z = 5.5 [type=bool, outer=(6), constraints=(/6: [/5.5 - /5.5]; tight), fd=()-->(6)]
└── filters
└── x > 'foo' [type=bool, outer=(1), constraints=(/1: [/e'foo\x00' - ]; tight)]

# Cardinality is zero.
build
DELETE FROM xyz WHERE False RETURNING *
----
delete xyz
project
├── columns: x:1(string!null) y:2(int!null) z:3(float)
├── fetch columns: x:4(string) y:5(int) z:6(float)
├── cardinality: [0 - 0]
├── side-effects, mutations
├── stats: [rows=0]
├── key: (1)
├── fd: (1)-->(2,3)
└── select
├── columns: x:4(string!null) y:5(int!null) z:6(float)
└── delete xyz
├── columns: x:1(string!null) y:2(int!null) z:3(float)
├── fetch columns: x:4(string) y:5(int) z:6(float)
├── cardinality: [0 - 0]
├── side-effects, mutations
├── stats: [rows=0]
├── key: (4)
├── fd: (4)-->(5,6)
├── scan xyz
│ ├── columns: x:4(string!null) y:5(int!null) z:6(float)
│ ├── stats: [rows=1000]
│ ├── key: (4)
│ └── fd: (4)-->(5,6)
└── filters
└── false [type=bool]
├── key: (1)
├── fd: (1)-->(2,3)
└── select
├── columns: x:4(string!null) y:5(int!null) z:6(float)
├── cardinality: [0 - 0]
├── stats: [rows=0]
├── key: (4)
├── fd: (4)-->(5,6)
├── scan xyz
│ ├── columns: x:4(string!null) y:5(int!null) z:6(float)
│ ├── stats: [rows=1000]
│ ├── key: (4)
│ └── fd: (4)-->(5,6)
└── filters
└── false [type=bool]
88 changes: 49 additions & 39 deletions pkg/sql/opt/memo/testdata/stats/insert
Original file line number Diff line number Diff line change
Expand Up @@ -43,61 +43,71 @@ select
├── side-effects, mutations
├── stats: [rows=66, distinct(1)=1, null(1)=0, distinct(2)=66, null(2)=0, distinct(3)=60.3661899, null(3)=0]
├── fd: ()-->(1)
├── insert xyz
├── project
│ ├── columns: x:1(string!null) y:2(int!null) z:3(float)
│ ├── insert-mapping:
│ │ ├── b:5 => x:1
│ │ ├── a:4 => y:2
│ │ └── c:6 => z:3
│ ├── side-effects, mutations
│ ├── stats: [rows=200, distinct(1)=1, null(1)=0, distinct(2)=200, null(2)=0, distinct(3)=130.264312, null(3)=2]
│ ├── fd: ()-->(1)
│ └── project
│ ├── columns: a:4(int!null) b:5(string!null) c:6(float)
│ ├── stats: [rows=200, distinct(4)=200, null(4)=0, distinct(5)=1, null(5)=0, distinct(6)=130.264312, null(6)=2]
│ ├── fd: ()-->(5)
│ └── select
│ ├── columns: a:4(int!null) b:5(string!null) c:6(float) rowid:7(int!null)
│ ├── stats: [rows=200, distinct(4)=200, null(4)=0, distinct(5)=1, null(5)=0, distinct(6)=130.264312, null(6)=2, distinct(7)=200, null(7)=0]
│ ├── key: (7)
│ ├── fd: ()-->(5), (7)-->(4,6)
│ ├── scan abc
│ │ ├── columns: a:4(int!null) b:5(string) c:6(float) rowid:7(int!null)
│ │ ├── stats: [rows=2000, distinct(4)=2000, null(4)=0, distinct(5)=10, null(5)=0, distinct(6)=200, null(6)=20, distinct(7)=2000, null(7)=0]
│ │ ├── key: (7)
│ │ └── fd: (7)-->(4-6)
│ └── filters
│ └── b = 'foo' [type=bool, outer=(5), constraints=(/5: [/'foo' - /'foo']; tight), fd=()-->(5)]
│ └── insert xyz
│ ├── columns: x:1(string!null) y:2(int!null) z:3(float)
│ ├── insert-mapping:
│ │ ├── b:5 => x:1
│ │ ├── a:4 => y:2
│ │ └── c:6 => z:3
│ ├── side-effects, mutations
│ ├── stats: [rows=200, distinct(1)=1, null(1)=0, distinct(2)=200, null(2)=0, distinct(3)=130.264312, null(3)=2]
│ ├── fd: ()-->(1)
│ └── project
│ ├── columns: a:4(int!null) b:5(string!null) c:6(float)
│ ├── stats: [rows=200, distinct(4)=200, null(4)=0, distinct(5)=1, null(5)=0, distinct(6)=130.264312, null(6)=2]
│ ├── fd: ()-->(5)
│ └── select
│ ├── columns: a:4(int!null) b:5(string!null) c:6(float) rowid:7(int!null)
│ ├── stats: [rows=200, distinct(4)=200, null(4)=0, distinct(5)=1, null(5)=0, distinct(6)=130.264312, null(6)=2, distinct(7)=200, null(7)=0]
│ ├── key: (7)
│ ├── fd: ()-->(5), (7)-->(4,6)
│ ├── scan abc
│ │ ├── columns: a:4(int!null) b:5(string) c:6(float) rowid:7(int!null)
│ │ ├── stats: [rows=2000, distinct(4)=2000, null(4)=0, distinct(5)=10, null(5)=0, distinct(6)=200, null(6)=20, distinct(7)=2000, null(7)=0]
│ │ ├── key: (7)
│ │ └── fd: (7)-->(4-6)
│ └── filters
│ └── b = 'foo' [type=bool, outer=(5), constraints=(/5: [/'foo' - /'foo']; tight), fd=()-->(5)]
└── filters
└── z > 1.0 [type=bool, outer=(3), constraints=(/3: [/1.0000000000000002 - ]; tight)]

# Cardinality is zero.
build
INSERT INTO xyz (x, y, z) SELECT b, a, c FROM abc WHERE False RETURNING *
----
insert xyz
project
├── columns: x:1(string!null) y:2(int!null) z:3(float)
├── insert-mapping:
│ ├── b:5 => x:1
│ ├── a:4 => y:2
│ └── c:6 => z:3
├── cardinality: [0 - 0]
├── side-effects, mutations
├── stats: [rows=0]
└── project
├── columns: a:4(int!null) b:5(string) c:6(float)
└── insert xyz
├── columns: x:1(string!null) y:2(int!null) z:3(float)
├── insert-mapping:
│ ├── b:5 => x:1
│ ├── a:4 => y:2
│ └── c:6 => z:3
├── cardinality: [0 - 0]
├── side-effects, mutations
├── stats: [rows=0]
└── select
├── columns: a:4(int!null) b:5(string) c:6(float) rowid:7(int!null)
└── project
├── columns: a:4(int!null) b:5(string) c:6(float)
├── cardinality: [0 - 0]
├── stats: [rows=0]
├── key: (7)
├── fd: (7)-->(4-6)
├── scan abc
│ ├── columns: a:4(int!null) b:5(string) c:6(float) rowid:7(int!null)
│ ├── stats: [rows=2000]
│ ├── key: (7)
│ └── fd: (7)-->(4-6)
└── filters
└── false [type=bool]
└── select
├── columns: a:4(int!null) b:5(string) c:6(float) rowid:7(int!null)
├── cardinality: [0 - 0]
├── stats: [rows=0]
├── key: (7)
├── fd: (7)-->(4-6)
├── scan abc
│ ├── columns: a:4(int!null) b:5(string) c:6(float) rowid:7(int!null)
│ ├── stats: [rows=2000]
│ ├── key: (7)
│ └── fd: (7)-->(4-6)
└── filters
└── false [type=bool]
Loading

0 comments on commit 6fd7498

Please sign in to comment.