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
SUMis checked as int64, and the statement fails on overflow (no wraparound).AVGon an integer column is rejected, because it would invent digits that do not exist. UseAVGonly ondecimalcolumns; it answers with the declared scale. Compute integer averages withSUMandCOUNT.
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 BYandSELECT *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/DESCper key. - NULL is the largest value (the PostgreSQL rule): last with
ASC, first withDESC. There is noNULLS FIRST/LASTsyntax. - 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. WithLIMIT, onlyoffset + limitrows 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 noSORTin 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 theWHEREequality column followed by theORDER BYcolumn, so it reads the index backward and stops (order=index_desc). If the sort column isvarchar, 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
OFFSETreads 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 asOFFSET(Tuning scenario 3).