NOT IN and NOT EXISTS (NOT NULL default and row budget)

In KDBS, columns are NOT NULL by default, so the NULL trap of NOT IN rarely arises. Instead, a NOT IN that rereads the subquery for every outer row hits the row budget (max_rows_touched) and is rejected. NOT EXISTS probes through an index for each outer row.

SQL

Situation

Among customers 1–2,000, find the customers with no orders in 2025.

Tables

TableColumnTypeDescription
customerscust_idint64Tuple key
orderscust_idint64Customer who placed the order. NOT NULL (KDBS default)
ordersord_dttimestampOrder date and time

Customers with no orders in 2025

BEFORE · NOT IN

SELECT *
FROM customers
WHERE cust_id BETWEEN 1 AND 2000
  AND cust_id NOT IN (SELECT cust_id
                      FROM orders
                      WHERE ord_dt >= '2025-01-01 00:00:00')

Run it and read the error message.

Approach

1. Check for the NULL trap

In other DBs, if the NOT IN subquery result contains even one NULL, the result is 0 rows. KDBS columns are NOT NULL unless declared NULL, so orders.cust_id cannot hold NULL. KDBS also evaluates NOT IN with three-valued logic, so if the subquery yields NULL from an expression (e.g. CASE … THEN NULL), the same trap exists.

2. See how many times the subquery runs

KDBS does not transform subqueries into joins. A subquery in condition position runs for every outer row, so if the subquery walks a large table, the work becomes outer rows × inner rows.

3. Make each outer row search by key

The correlation condition o.cust_id = c.cust_id of NOT EXISTS can be probed through orders_x1(cust_id, …).

Explanation

NOT IN → NOT EXISTS

AFTER · NOT EXISTS

SELECT *
FROM customers AS c
WHERE c.cust_id BETWEEN 1 AND 2000
  AND NOT EXISTS (SELECT o.ord_id
                  FROM orders AS o
                  WHERE o.cust_id = c.cust_id
                    AND o.ord_dt >= '2025-01-01 00:00:00')

Why it works

  • Before is rejected with statement exceeded its row-touch budget of 3000000 tuples. It rewalks the 2025 orders (530,000 rows) for every customer, so it needs 2,000 × 530,000 rows. KDBS caps the number of rows a statement interprets so that a statement does not block other sessions (common lab environment).
  • In After, for each row of Range customers AS c range=[1, 2000] (Buffers 29), IndexProbe orders AS o key=… finds that customer's orders (opened 2,000 times). Result 17 rows, total Buffers 176,209.
  • Buffers is large because of customers 1–100 (heavy customers, 2,000 or more orders each). The probe enters the index only by cust_id, and the ord_dt condition is used to filter the rows found, so for these customers it reads from the oldest orders until it finds a 2025 order.

Check it yourself

TRY · Range without heavy customers

SELECT *
FROM customers AS c
WHERE c.cust_id BETWEEN 101 AND 2000
  AND NOT EXISTS (SELECT o.ord_id
                  FROM orders AS o
                  WHERE o.cust_id = c.cust_id
                    AND o.ord_dt >= '2025-01-01 00:00:00')
  • Excluding customers 1–100 drops Buffers to 28,465, with the same 17 rows. 84% of the cost came from the 100 heavy customers.

Key points

  • Correctness: the KDBS NOT NULL default prevents the NOT IN NULL trap at the schema level. Even so, NOT EXISTS has a clear meaning regardless of NULL.
  • Cost: in KDBS, which has no subquery transformation, a subquery must have a shape that searches by key for each outer row (EXISTS/NOT EXISTS with an indexed correlation condition).
  • The row budget rejection message also tells the direction of the fix: "a correlated subquery or a join on a non-tuple-key column reads the inner side for each outer row".