Top-N partial range processing (sort elimination)
The 10 most recent paid orders. Instead of sorting everything, LIMIT and index order (order=index_desc) stop at 10 rows. Sorts in KDBS do not spill to disk, so a large sort does not run slowly; it fails.
SQL
Situation
The admin home screen shows the 10 most recent paid orders. The query fetches all paid orders newest first, and the application uses only the first 10.
Tables
| Table | Column | Type | Description |
|---|---|---|---|
orders | status | varchar(16) | PAID has 140,499 rows |
orders | ord_dt | timestamp | Order date and time |
| Index | Columns |
|---|---|
orders_x2 | status, ord_dt |
Recent paid orders
BEFORE · Full sort
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC
Run it and see how many rows the SORT row received and what Buffers is.
Approach
1. Put the number of rows needed in the SQL
Even if the app uses only 10 rows, the engine builds the whole result when the SQL does not say so.
2. Check whether an index holds the sort order
WHERE status = 'PAID' ORDER BY ord_dt DESC matches the order of reading orders_x2(status, ord_dt) backward. When an index starts with the WHERE equality columns and the ORDER BY columns follow exactly, KDBS reads in index order instead of sorting and stops, under a LIMIT.
Explanation
Full sort → LIMIT
AFTER · LIMIT 10
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY ord_dt DESC
LIMIT 10
Why it got faster
- In Before,
IndexProbefinds all 140,499PAIDrows andSORTsorts them. Each row found through the index reads a table page, so Buffers is 140,500. - After is
IndexProbe … order=index_descunderQUOTA limit=10. There is noSORTrow. It reads 10 rows from the end of thePAIDsegment of the index and stops. Buffers 11.
Variation
TRY · Top 10 by amount
SELECT *
FROM orders
WHERE status = 'PAID'
ORDER BY amt DESC
LIMIT 10
- No index holds
amtorder, so even withLIMITall 140,499 rows are read and sorted (Buffers 140,500).LIMITreduces only the rows received and the memory the sort holds (10 rows), not the amount read.
TRY · String column order is not replaced by an index
SELECT *
FROM orders
WHERE cust_id = 101
ORDER BY ord_dt DESC
LIMIT 5
order=index_descwithorders_x1(cust_id, ord_dt), Buffers 6. With the same shape, if theORDER BYcolumn isvarchar, KDBS does not use index order. The index keeps only the leading part of a string as its key.
When a sort overflows in KDBS
KDBS sorts only in memory. When the rows a sort holds exceed sort_max_rows (default 1,048,576), the sort does not spill to disk and the statement fails (ckdbs/manual/sql/sql.md §4). With LIMIT, the sort holds only offset + limit rows. A "slow query" in another DB can be a "failing query" in KDBS.
Key points
- Top-N stops at the first N rows only when
LIMITand a sorted index are both present. - When
ANALYZEhas nosortline and a step carriesorder=index_asc,order=index_desc, ororder=tuple_key_desc, the sort was eliminated. - An ascending sort on the tuple key is always free; a descending one is free when there is a
LIMIT.