Interview: login looks up an email and reads the whole customers table
A secondary index leaf is the email plus a pointer to cust_id. Seek, then fetch, unless the leaf covers the columns login actually needs.
More on Ticket interviews
- Database Interviews in the AI Era: The Ticket Format
- Interview: the same job runs twice
- Deadlock Interview Question: Find and Fix It in SQL
- Slow Query Interview: One Keyword Made It 40× Slower
- Interview: rate limit is 2/min, 30 requests get through
- Interview: “just add an index” slowed ingestion
- Interview: “I changed my name, refresh shows the old one”
- Interview: importing 10M rows takes six hours
- Interview: everything got slow and the disk keeps growing
- Lost Update Interview: Wallet Balance Wrong Under Load
- Interview: API p99 jumps 10× every night at 2 am
- Interview: profile lookups got 40× slower, writes are fine
- Interview: inserts stall, and only one page is hot
- Interview: the write succeeded, the next read shows an empty cart
- Interview: inventory stayed locked after the coordinator restarted
- Interview: for four minutes, both regions took orders
- Interview: shoes show a 0% discount, then the report crashes
- Interview: March gained $50 after the deploy, and the months don’t add up
- Interview: the new branch’s dashboard takes 38 seconds
- Interview: the index is right there, make Postgres use it
- Interview: warehouse and checkout overwrite the same order
- Interview: the bestsellers report now takes nine minutes
- Interview: an analyst query locked checkout
- Interview: the member is active and the email went out twice
- Interview: one shard is at 96% CPU and the other is idle
- Interview: the menu is slow, and the proposal is event sourcing
- Interview: four dashboards disagree about whether the database is slow
- Interview: checkout got slow the hour the warehouse job started
- Interview: the query is fast in staging and slow in production
- Interview: the proposal splits one database into three shards
- Interview: the order page joins four tables on every view
- Interview: every page view is a read-write query on the leader
- Interview: Sarah and John both debit version 1
- Interview: the CTO read a blog post and wants to move Postgres to MySQL
- Interview: one table takes 50,000 writes a second, another feeds dashboards. Which engine for each?
- Interview: EU users wait on the ocean for every write, can both regions take writes?
- Interview: users search cars by color, but the table is sharded by car id
- Interview: we added a fourth node and most of the data moved
- Interview: two people book the same room at noon, both get a confirmation
- Interview: everything is on the strongest consistency and far requests crawl
- Interview: the order is saved, the message to search and email never left
- Interview: rename a column on a 600 GB table while 30 servers keep serving traffic
- Interview: orders go to 16 shards in 2 regions, what should the order id be?
- Interview: one celebrity posts, and every timeline in the app slows down
- Interview: 5,000 customers share one database, and one of them slows down everyone
Keep learning
Login looks up an email and reads the whole customers table
Login searches by email, but the only index is on cust_id. What does an email index actually store? secondary index write cost Every login scans all 40 million customers. Unique index on email, covering only what login reads.
POST /login scans 40 million customers
cust_id is the primary key. Email has no index. A proposal: INDEX (email) plus SELECT * (incl. a big profile document). 40 million seq scan email = Restated: the filter does not match the index we have. We need emails in sorted order.
Four checks, in order
Does an index for this filter exist? compare filter to index keys No. The primary key cannot find an email. The scan is honest. What would the new index store? describe one leaf entry Email plus a pointer (the cust_id). Seek email, then fetch the row. Does login need the fetch? list the columns used Only if the hash is missing from the index. SELECT * always fetches. What does the index cost on writes? count trees per insert One extra tree per insert. Tiny next to a 40M-row scan per login. rows read per login today row read with an email index Seek email email index Leaf: email → cust_id the pointer Fetch row primary key A secondary index lookup: seek, then follow the pointer.
You describe the leaf, not the word “index”
Says what the leaf stores: key plus pointer. Keeps the big document out of the login read. Enforces unique emails, since an email is an identity.
Say these without the model
Walk the read. Email alice@example.com is in the index with pointer 2. Then what? Follow pointer 2 into the primary key and read that one row. The index is sorted by email, so the seek finds alice fast. Two lookups, two structures. The 40M-row scan is gone. The one-row fetch stays, unless the index covers the query. seek then fetch one row, not the table seek in email index PK lookup row 2, all columns When is the pointer lookup unnecessary? When the index holds every column the query needs (a covering index). Then the answer comes from the leaf. No table read. SELECT * never covers: the leaf lacks the profile document. Covering is wider and slower to write. Use it when the fetch hurts. columns in the leaf SELECT * defeats it wider leaf, costlier write SELECT * Seek, then fetch the row and its document. SELECT cust_id, hash + INCLUDE Answer from the leaf. No fetch. Why can the right index still be the wrong one? selectivity and order If the query cannot use its sort order. (last_name, email) cannot seek by email alone. The leftmost column leads. A filter matching most rows may be cheaper as a scan. leftmost prefix expression hides the key Wrong leading column (last_name, email) Wrapped column LOWER(email), LIKE '%x' Matches most rows Scan is cheaper What do you give up by adding this index? write cost Slower writes and more memory. Worth it for login. Each insert or email change also writes the index: more log, more pages. A cold index after restart makes the first logins slower. Login is hot and returns one row. The trade pays off. another tree per write this predicate earns it
How wide should the email index be?
Login reads cust_id and password_hash. The row also has a profile document. UNIQUE (email) INCLUDE (password_hash), select only those columns One seek, the hash is in the leaf, and the document stays on disk. Uniqueness comes free. Login reads Write amplification Index width INDEX (email) and SELECT * The scan is gone, but every login fetches the whole row. Correct, with extra I/O. INDEX (email, profile_document, password_hash, …) You copied the table into the index. Every profile edit rewrites a huge leaf.
Have one ready
Draw a secondary index. What is in the leaf, and when do you still read the table? Value + pointer (usually the PK). The extra tree, and why it paid off.
Did you show these?
PK cannot seek by email Seek vs fetch No SELECT * with a document Named the extra write cost
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.