KDBS: Aggregation, Sorting, Paging

Aggregation

COUNT, SUM, MIN, MAX, and AVG are available, and all accept DISTINCT. The step chain answers GROUP BY on columns; the query layer answers HAVING, aliases on aggregates, and grouping by expressions.

KDBS · Count and total by status

SELECT status, COUNT(*), SUM(amt), MIN(amt), MAX(amt)
FROM orders
WHERE cust_id = 101
GROUP BY status
  • SUM is checked as int64, and the statement fails on overflow (no wraparound).
  • AVG on an integer column is rejected, because it would invent digits that do not exist. Use AVG only on decimal columns; it answers with the declared scale. Compute integer averages with SUM and COUNT.

KDBS · Integer AVG

SELECT AVG(amt) FROM orders WHERE cust_id = 101

KDBS · SUM / COUNT

SELECT SUM(amt), COUNT(*), SUM(amt) / COUNT(*) AS avg_amt
FROM orders
WHERE cust_id = 101
  • A non-grouped column in the select list is rejected (there is no mode that picks "any row"). GROUP BY and SELECT * cannot be used together either.

KDBS · HAVING (query layer)

SELECT cust_id, COUNT(*)
FROM orders
WHERE cust_id BETWEEN 95 AND 105
GROUP BY cust_id
HAVING COUNT(*) >= 100

ORDER BY

KDBS · Multiple keys

SELECT ord_id, status, amt
FROM orders
WHERE cust_id = 101
ORDER BY status, amt DESC
  • Up to 8 keys, with ASC/DESC per key.
  • NULL is the largest value (the PostgreSQL rule): last with ASC, first with DESC. There is no NULLS FIRST/LAST syntax.
  • Sorting happens only in memory. If the rows the sort must hold exceed sort_max_rows (default 1,048,576), the statement fails instead of spilling to disk. With LIMIT, only offset + limit rows are held.
  • Without ORDER BY, row order is not guaranteed, because the engine decides the join order.

Free sorts

KDBS · Tuple key ascending

SELECT ord_id, ord_dt FROM orders WHERE ord_id BETWEEN 1000 AND 1005 ORDER BY ord_id
  • The table is stored in tuple key order, so the compiler removes ORDER BY ord_id. There is no SORT in the execution plan table.

KDBS · Tuple key descending + LIMIT

SELECT ord_id, ord_dt FROM orders ORDER BY ord_id DESC LIMIT 5
  • With LIMIT, it reads from the end of the table and stops at 5 rows (order=tuple_key_desc).

KDBS · Index order + LIMIT

SELECT ord_id, ord_dt FROM orders WHERE cust_id = 101 ORDER BY ord_dt DESC LIMIT 5
  • orders_x1(cust_id, ord_dt) starts with the WHERE equality column followed by the ORDER BY column, so it reads the index backward and stops (order=index_desc). If the sort column is varchar, this method is not used.

LIMIT and OFFSET

The order is ORDER BY … LIMIT n OFFSET m. OFFSET m LIMIT n is rejected. There is no FETCH FIRST, TOP, or ROWNUM.

KDBS · OFFSET LIMIT order

SELECT ord_id FROM orders ORDER BY ord_id DESC OFFSET 10 LIMIT 5

KDBS · Deep OFFSET

SELECT ord_id, ord_dt FROM orders ORDER BY ord_id DESC LIMIT 5 OFFSET 100000
  • OFFSET reads all the rows it skips (Buffers 1,206).

KDBS · Closed keyset window

SELECT ord_id, ord_dt
FROM orders
WHERE ord_id <= 1900000 AND ord_id > 1899995
ORDER BY ord_id DESC
LIMIT 5
  • Continue reading from the last key seen, but write it as a window closed at both ends. A window bounded at only one end (WHERE ord_id <= 1900000) walks down from the end of the table while filtering, so it reads as much as OFFSET (Tuning scenario 3).