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

TableColumnTypeDescription
ordersstatusvarchar(16)PAID has 140,499 rows
ordersord_dttimestampOrder date and time
IndexColumns
orders_x2status, 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, IndexProbe finds all 140,499 PAID rows and SORT sorts them. Each row found through the index reads a table page, so Buffers is 140,500.
  • After is IndexProbe … order=index_desc under QUOTA limit=10. There is no SORT row. It reads 10 rows from the end of the PAID segment 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 amt order, so even with LIMIT all 140,499 rows are read and sorted (Buffers 140,500). LIMIT reduces 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_desc with orders_x1(cust_id, ord_dt), Buffers 6. With the same shape, if the ORDER BY column is varchar, 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 LIMIT and a sorted index are both present.
  • When ANALYZE has no sort line and a step carries order=index_asc, order=index_desc, or order=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.