ZZomato·BehavioralL2DSA Round

Database Indexing for Simple and Composite Keys

viaGlassdoor

Question: How does indexing work for simple keys versus composite keys in a database?

Answer outline: A simple-key index (e.g. on a single column like user_id) is typically a B+ Tree ordering rows by that one column's value, giving O(log n) lookups on equality/range queries on that column. A composite-key index (on multiple columns, e.g. (city_id, created_at)) orders rows lexicographically by the first column, then the second, etc. — it speeds up queries filtering on a leading prefix of the composite key (e.g. city_id alone, or city_id + created_at together) but does NOT help queries that filter only on a non-leading column of the composite index.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account