Preparing your design workspace

Loading the problem and your latest saved attempt…

Relational Query Index Design

Design and validate one composite B-tree index for a high-volume, stably paginated order search. Defend the same equality-before-order reasoning for a focused latest-new-job query.

Problem statement

For this exercise, assume an order-query service reads a multi-tenant relational orders table with tenant_id, order_id, status, created_at, customer_id, and total_cents.

Its primary query filters one tenant and one status over a half-open created-at time range, returns the newest rows ordered by created_at descending and order_id descending, and limits each page to 50 rows. Later pages use an opaque keyset cursor. You may add one composite B-tree index.

Design the index key order and direction, cursor semantics, rollout and plan validation, and operational monitoring. Preserve ordinary order writes and explain row-lookup versus covering trade-offs without relying on a vendor-specific included-column feature.

Exclude joins, aggregation, full-text search, database-engine implementation, and unrelated query shapes. The explicitly named latest-new-job review below is the only additional follow-up.

Focused latest-new-job follow-up: explain how to optimize SELECT * FROM job WHERE status='new' ORDER BY created_on DESC LIMIT 1 on a table with 100 million rows, of which about 20,000 have status new. Work on the database access path before proposing a cache.

Explain a composite-index column order and how you would validate the resulting plan. This is a separate, simpler job-table workload: do not add the order-service tenant filter, time-window predicate, cursor or order_id tie breaker.

For this follow-up, assume status and created_on are non-null, created_on is a comparable timestamp, one statement reads one committed database view, any one tied newest qualifying row is acceptable, and no match returns an empty result.

The other payload columns returned by SELECT * are unspecified; do not assume a covering index. No source latency, throughput, write rate or database engine is supplied for this workload, so keep any such target conditional on an explicit practice assumption rather than borrowing the order-service targets.

Creating, claiming, dispatching or updating jobs is outside scope. The source reports the question and the database-first focus; the input/tie/empty-result conventions above are bounded practice choices.

What to build

  1. Return the first page of orders for one tenant, status, and half-open created-at range in deterministic newest-first order.
  2. Return later pages from an opaque keyset cursor without duplicate rows when new orders arrive after the first page.
  3. Use one composite B-tree index to bound the scanned key range and avoid a separate sort for the supported query. In the separate latest-new-job follow-up, explain a query-aligned job-table index without changing the primary order-search contract.
  4. Build, validate, observe, and roll back the index without changing order correctness or overwhelming the write path.

At scale

  • Assume the table contains 100 million orders and receives 5,000 supported queries and 1,000 writes per second at peak.
  • Assume a common tenant-status time range matches between 10 and 10,000 rows.
  • Assume first and continuation pages should complete within 100 milliseconds at p95 and return at most 50 rows.
  • Assume created_at is immutable after insertion, order_id is unique, and the added index must remain online while ordinary inserts and status updates continue.