Skip to content
Discussion options

You must be logged in to vote

You’ve hit the classic indexing dilemma – speed vs write overhead. There’s no single perfect index, but here’s a practical approach for PostgreSQL / MySQL (similar logic applies elsewhere)

1. Replace single‑column indexes with composite ones

Your single indexes on user_id and created_at are partially useful but not optimal. The database can only use one index per table reference in most plans (unless bitmap scan). Create composite indexes that match the most common filtering order:

Query pattern Best composite index
WHERE user_id = ? AND created_at > ? (user_id, created_at) – user_id first (equality), then created_at (range)
WHERE status = ? AND created_at > ? (status, created_at) – statu…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tirexxic
Comment options

Answer selected by tirexxic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants