Back to Course

Practice · Scaling · Card 1

Which single index helps this query the most?

A 5M-row table, a filter, a sort, a small limit. The right index choice can make this query 100x faster.

The query

The dashboard query, against a 5M-row posts table. Only a small fraction have status = "pending".

Post.where(status: "pending")
    .order(:created_at)
    .limit(20)

# Generates:
# SELECT "posts".* FROM "posts"
# WHERE "posts"."status" = 'pending'
# ORDER BY "posts"."created_at" ASC
# LIMIT 20

The question

Which index choice gives you the best plan for this query? Assume Postgres, B-tree indexes.

Take a moment. Pick the best answer. Wrong picks reveal why they're wrong, which is half the point.