KDBS: Joins, Subqueries, and the Query Layer

KDBS executes a statement along one of two paths (Step chain and ANALYZE).

PathStatements acceptedExecution plan table
Step chainSingle table, inner equi-join (JOIN … ON a.x = b.y), subqueries in condition positions (IN, EXISTS, comparison), GROUP BY on columns and aggregates, ORDER BY/LIMITStarts, A-Rows, Buffers per step
Query layerOuter joins, CROSS JOIN, multiple conditions in ON, set operations, derived tables, expressions and scalar subqueries in the select list, HAVING, aliases on aggregatesEmpty (reason in the Raw tab)

Inner joins (step chain)

KDBS · Orders and customer names

SELECT o.ord_id, o.ord_dt, o.amt, c.cust_nm, c.grade
FROM orders AS o
JOIN customers AS c ON c.cust_id = o.cust_id
WHERE o.cust_id = 101
ORDER BY o.ord_id
  • Write a single equality in ON, and qualify the columns on both sides with the table name or alias.
  • If the join key is the inner table's tuple key (customers.cust_id), it is a probe that descends once per row: Probe customers … key=… in the execution plan table.
  • The engine decides the join order (STEP CHAIN order=…). It may differ from the written order (Join order and plans).

KDBS · Three tables and an aggregate

SELECT g.grade_nm, COUNT(*), SUM(o.amt)
FROM customers AS c
JOIN grade_cd AS g ON g.grade = c.grade
JOIN orders AS o   ON o.cust_id = c.cust_id
WHERE c.cust_id BETWEEN 101 AND 300
GROUP BY g.grade_nm

A self-join, which uses the same table twice, takes different aliases. FROM t JOIN t is rejected.

KDBS · Self-join

SELECT a.ord_id, b.ord_id, a.ord_dt, b.ord_dt
FROM orders AS a
JOIN orders AS b ON b.ord_id = a.ord_id
WHERE a.ord_id BETWEEN 10 AND 12

Subqueries in condition positions (step chain)

KDBS · EXISTS

SELECT c.cust_id, c.cust_nm
FROM customers AS c
WHERE c.cust_id BETWEEN 5001 AND 5010
  AND EXISTS (SELECT o.ord_id FROM orders AS o
              WHERE o.cust_id = c.cust_id AND o.amt > 450000)
  • The subquery runs once per outer row (IndexProbe orders AS o Starts 10, Buffers 121). KDBS does not transform subqueries into joins. So the correlation condition (o.cust_id = c.cust_id) must have a shape that an index or the tuple key can look up.
  • Nesting goes up to 4 levels.
  • If the inner table has two or more conditions, the engine decides which index to enter through. Changing o.amt > 450000 in the statement above to o.status = 'CANCELED' makes the inner side enter through orders_x2(status, …), reading 60,000 canceled orders per customer, for Buffers 429,112 across 10 customers (measured). That is why it is not included as a runnable block.

KDBS · Comparison subquery

SELECT ord_id, amt
FROM orders
WHERE cust_id = 101
  AND amt > (SELECT amt FROM orders WHERE ord_id = 5)
  • A subquery not correlated with the outer query is also re-executed for each outer row (Lookup orders Starts 15). Here the inner side is one tuple-key row, so it is cheap, but if the inner side walks a large table, the cost becomes outer rows × inner rows.
  • If the subquery contains an aggregate (MIN, COUNT) or GROUP BY, the query layer answers it.

Query layer

KDBS · Outer join

SELECT c.cust_id, c.cust_nm, o.ord_id
FROM customers AS c
LEFT JOIN orders AS o ON o.cust_id = c.cust_id
WHERE c.cust_id BETWEEN 1 AND 3

This statement fails: this statement's relation walk would hold more than sort_max_rows (1048576) rows. The layer fetches rows for each table with the WHERE conditions that refer only to that table, and orders has no such condition, so it would have to fetch 2 million rows. Putting a WHERE condition on the inner side of an outer join (the side filled with NULLs) changes the meaning of the outer join, so the layer does not apply that condition first either.

If the inner table is small, the outer join works.

KDBS · Outer join with a small table

SELECT g.grade, g.grade_nm, c.cust_id
FROM grade_cd AS g
LEFT JOIN customers AS c ON c.grade = g.grade AND c.cust_id = 100
  • The layer fetches the 100,000 customers rows and hashes them. The fetched rows are within sort_max_rows, so it runs. c.cust_id = 100 in ON is a matching condition, so grades without a match remain.

KDBS · UNION ALL

SELECT ord_id, status FROM orders WHERE ord_id BETWEEN 1 AND 3
UNION ALL
SELECT ord_id, status FROM orders WHERE ord_id BETWEEN 1999998 AND 2000000

KDBS · Derived table

SELECT t.status, t.amt
FROM (SELECT status, amt FROM orders WHERE cust_id = 101) AS t
WHERE t.amt > 300000

KDBS · Scalar subquery (narrowed range)

SELECT c.cust_id,
       (SELECT COUNT(*) FROM orders AS o WHERE o.cust_id = c.cust_id AND o.cust_id BETWEEN 101 AND 105) AS cnt
FROM customers AS c
WHERE c.cust_id BETWEEN 101 AND 105

Summary

  • Write large tables that must be reached by key in a shape the step chain answers: inner equi-joins, EXISTS/IN in condition positions, aggregates without aliases.
  • For query-layer statements, put conditions that refer only to each table on every table to reduce the rows fetched. If the rows to hold exceed 1 million, it is an error.
  • ANALYZE does not explain query-layer statements, so the sandbox's execution plan table is empty.