Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
131383: opt: fix greedy lookup join constraints r=mgartner a=mgartner

#### opt: lazily convert lookup join equality columns to lookup expr

Previously, the lookup join constraint builder would immediately convert
equality columns to an equality expression in the lookup expression when
it was determined that a lookup expression was required to perform the
lookup join. Now, this conversion happens after all the equality columns
and the lookup expression have been collected. This simplifies the
logic.

Release note: None

#### opt: fix greedy lookup join constraints

Previously, lookup joins would be planned that constrained as many index
columns as possible. This is not optimal when a suffix of columns are
constrained to multiple values or a range of constant values by optional
filters. These suffixes increase the number of lookup spans without
making the constraint more selective. In the case where suffix columns
are constrained to multiple values, a lookup span is generated for each
value, significantly increasing the number of lookups performed for each
input row.

This commit makes the building of lookup join constraints less greedy.
The suffix of constrained columns will only be constrained to multiple
values or a range over some constant values if those constraints
originate from the query filter.

Fixes cockroachdb#75596

Release note (performance improvement): The optimizer now plans more
efficient lookup joins in some cases.


Co-authored-by: Marcus Gartner <[email protected]>
  • Loading branch information
craig[bot] and mgartner committed Oct 3, 2024
2 parents 7a0d27e + bc493bf commit 06f78c6
Show file tree
Hide file tree
Showing 16 changed files with 355 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ SELECT * FROM [EXPLAIN UPSERT INTO regional_by_row_table (crdb_region, pk, pk2,
│ │
│ └── • lookup join (semi)
│ │ table: regional_by_row_table@new_idx
│ │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (column4 = a)) AND (b > 0)
│ │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (b > 0)) AND (column4 = a)
│ │ pred: (upsert_pk != pk) OR (column1 != crdb_region)
│ │
│ └── • filter
Expand Down Expand Up @@ -1767,7 +1767,7 @@ VALUES ('us-east-1', 23, 24, 25, 26), ('ca-central-1', 30, 30, 31, 32)] OFFSET 2
│ │
│ └── • lookup join (semi)
│ │ table: regional_by_row_table@new_idx
│ │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (column4 = a)) AND (b > 0)
│ │ lookup condition: ((crdb_region IN ('ap-southeast-2', 'ca-central-1', 'us-east-1')) AND (b > 0)) AND (column4 = a)
│ │ pred: (upsert_pk != pk) OR (column1 != crdb_region)
│ │
│ └── • filter
Expand Down
30 changes: 15 additions & 15 deletions pkg/sql/opt/exec/execbuilder/testdata/lookup_join
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def@def_pkey
│ lookup condition: (b = f) AND (e > 1)
│ lookup condition: (e > 1) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -106,7 +106,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def@desc_idx
│ lookup condition: (b = f) AND (e > 1)
│ lookup condition: (e > 1) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -126,7 +126,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def@def_pkey
│ lookup condition: (b = f) AND (e > 9223372036854775807)
│ lookup condition: (e > 9223372036854775807) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -146,7 +146,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def_e_decimal@def_e_decimal_pkey
│ lookup condition: (b = f) AND (e > 1)
│ lookup condition: (e > 1) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -166,7 +166,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def_e_decimal@desc_idx
│ lookup condition: (b = f) AND (e >= 1)
│ lookup condition: (e >= 1) AND (b = f)
└── • scan
columns: (a, b, c)
Expand Down Expand Up @@ -225,7 +225,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def@def_pkey
│ lookup condition: (b = f) AND (a >= e)
│ lookup condition: (a >= e) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -244,7 +244,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def@desc_idx
│ lookup condition: (b = f) AND (a >= e)
│ lookup condition: (a >= e) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -263,7 +263,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def@def_pkey
│ lookup condition: (b = f) AND (a > e)
│ lookup condition: (a > e) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -282,7 +282,7 @@ vectorized: true
│ columns: (a, b, c, d, e, f)
│ estimated row count: 33
│ table: def@desc_idx
│ lookup condition: (b = f) AND (a > e)
│ lookup condition: (a > e) AND (b = f)
└── • scan
columns: (a, b, c)
Expand All @@ -304,7 +304,7 @@ vectorized: true
│ columns: (column11, a, b, c, d, e, f)
│ estimated row count: 33
│ table: def_e_decimal@def_e_decimal_pkey
│ lookup condition: (b = f) AND (column11 >= e)
│ lookup condition: (column11 >= e) AND (b = f)
└── • render
│ columns: (column11, a, b, c)
Expand Down Expand Up @@ -333,7 +333,7 @@ vectorized: true
│ columns: (column11, a, b, c, d, e, f)
│ estimated row count: 33
│ table: def_e_decimal@desc_idx
│ lookup condition: (b = f) AND (column11 >= e)
│ lookup condition: (column11 >= e) AND (b = f)
└── • render
│ columns: (column11, a, b, c)
Expand Down Expand Up @@ -2186,14 +2186,14 @@ vectorized: true
·
• lookup join (left outer)
│ table: lookup_expr@lookup_expr_r_x_y_z_w_idx
│ lookup condition: ((((r IN ('east', 'west')) AND (column2 = x)) AND (y IN (10, 20))) AND ("lookup_join_const_col_@8" = z)) AND (column1 = w)
│ lookup condition: ((((r IN ('east', 'west')) AND (y IN (10, 20))) AND (column2 = x)) AND ("lookup_join_const_col_@8" = z)) AND (column1 = w)
└── • render
└── • values
size: 2 columns, 3 rows
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyUUm1P20AM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C9oUQfhgfEUiPQHZxsiRUtfk9QZv0ltu565X8AYrOkt6_1bnKyzGnGVe73uvN4oc5i5V0o6t4OG4FRMINCeVGnFRJkzwkj8kiybMGCYO2Bt9VXoIqBTiO48g9sHfd4lSbLSFEFI_98DPB1oI3E-7zIeRR2-511lJ0X0J33dODE5CHa-hZeLyGXQsPrK6nqqhnpRZQEfwksM4geCB4RMJRbQTILskeyT2S-yQPSB6SPCJ5_E_dvT909_5H9zHruSo1v9L8vRfe9pYTQs5ueWVBrepqypeVmra9KzhqidpExtqsqt4K-OVLSZuK09naNptM3ptM3beYJoQ3hbJOQIGd57f9l8_LQ_tDequtRNGdWrS01oEaxU1aaCa8SO95wIarWV7m2uRTFKaqebn88CsAAP__a61CLw==

query T
EXPLAIN (DISTSQL) SELECT * FROM (VALUES (1, 10), (2, 20), (3, NULL)) AS u(w, x) WHERE NOT EXISTS (
Expand All @@ -2205,14 +2205,14 @@ vectorized: true
·
• lookup join (anti)
│ table: lookup_expr@lookup_expr_r_x_y_z_w_idx
│ lookup condition: ((((r IN ('east', 'west')) AND (column2 = x)) AND (y IN (10, 20))) AND ("lookup_join_const_col_@8" = z)) AND (column1 = w)
│ lookup condition: ((((r IN ('east', 'west')) AND (y IN (10, 20))) AND (column2 = x)) AND ("lookup_join_const_col_@8" = z)) AND (column1 = w)
└── • render
└── • values
size: 2 columns, 3 rows
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJyUUt1O20wQvf-eYjQ32J8WyDqlRSshOSWmNTIOjQ1FqiLLigdwcbzp7loxoLx7tQ64oWoR3YvVnPk5Mzozj6h_VCgwuDqPRmEMzjhM0uRL5EISRMFxCv_DyXRyBs7lKLoIEnA4Az5wGTgeA68zhgziiyhyXRgl0ICzYtC68PVzMA0gnqQQXFlKcF4SVlLeNcuM2qWydeapwGn2VnAEZm_lwigeW9x2uHVdZFjLguJ8QRrFN-Q4Y7hUck5aS2Vdj11CWLQoBgzLetkY654xnEtFKB7RlKYiFHiZVw3p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UpM0myjcgrJ9D2ijKF_2BbDPxV5m815hmDK8raZeOAgdPb_cP3_NDW5DfaCtRcitXHa29NY3iOq80MTzL72hMhtSirEttyjkKoxpar__7GQAA__9bSkP1

# The following tests check that if the joiners can separate a row request
# into separate families that it does, and generates spans for each family
Expand Down
Loading

0 comments on commit 06f78c6

Please sign in to comment.