Indexes
34 questions in 12 subtopics.
B-tree indexes
How is a B-tree index organised on disk, and why can it answer equality, range (BETWEEN, <, >), ORDER BY and prefix LIKE 'abc%' queries but not LIKE '%abc'?Query: SELECT * FROM orders WHERE created_at >now() - interval '1 day' ORDER BY created_at DESC LIMIT 50. There is a B-tree index on created_at, but EXPLAIN shows a Seq Scan and a Sort on a 200 million row table after a large backfill. What do you check and how do you fix it?A team wants a policy that every column used in any WHERE clause gets its own single-column B-tree index. As the database lead for 30 services, how do you respond and what standard do you propose instead?Over 20 years, index advice moved from DBAs hand-tuning a few big databases to developers adding indexes in migrations across hundreds of services, and now to managed databases with automatic index suggestions. What does this mean for how you staff and govern database performance today?Hash indexes
What can a hash index do that a B-tree cannot, and what can it not do? When is it worth using one in PostgreSQL, and what does MySQL InnoDB offer instead?A table stores 300 million rows with a url column averaging 400 bytes, queried only by exact url match. The B-tree on url is 140 GB and does not fit in memory. What options do you compare?Composite indexes and column order
Given index idx ONorders(customer_id, status, created_at), which of these queries can use it efficiently and how? (a) WHERE customer_id = 7 (b) WHERE status = 'NEW' (c) WHERE customer_id = 7 AND created_at > '2026-01-01' (d) WHERE customer_id = 7 AND status = 'NEW' ORDER BY created_at DESC LIMIT 20A multi-tenant SaaS table has indexes on (tenant_id), (tenant_id, created_at), (created_at, tenant_id) and (tenant_id, user_id, created_at). Writes are slow. How do you decide which to keep?Your company is designing the indexing standard for a shared multi-tenant schema used by 25 teams. Should tenant_id lead every composite index? Discuss the trade-offs, including partitioning and row-level security.Covering indexes and index-only scans
What is a covering index, and how does PostgreSQL's INCLUDE clause differ from adding the column as an extra key column? Why might EXPLAIN show Index Only Scan with Heap Fetches: 90000?A dashboard endpoint runs SELECT status,count(*) FROM tickets WHERE project_id = ? GROUP BY status every few seconds for thousands of users. The table has 500 million rows and is updated constantly. How would you make it fast?Teams keep adding wide covering indexes to make specific API endpoints fast, and a 2 TB orders table now has 1.6 TB of indexes. At company scale, when should read performance be solved by indexes, and when by a different data shape such as read models, caches or an analytics store?Partial and expression indexes
Write PostgreSQL indexes for these two needs and show a query that uses each: (1) a jobs table where 99 percent of rows have status = 'DONE' but workers pollWHERE status = 'PENDING' ORDER BY created_at; (2) case-insensitive login lookup by email.A Spring Data JPA query findByEmailIgnoreCase(String email) is slow even though you created CREATE INDEX ON users (lower(email)). EXPLAIN of the logged SQL shows WHERE upper(u1_0.email) = upper(?). Why, and what are the fixes?Clustered vs heap tables
Compare InnoDB's clustered index (index-organised table) with PostgreSQL's heap tables. How does a secondary index lookup work in each, and what does PostgreSQL's CLUSTER command really do?An InnoDB events table uses a random UUID (CHAR(36)) primary key. Insert throughput fell by 70 percent once the table grew past the buffer pool size, and secondary indexes are huge. Explain why and plan the fix.Your organisation runs both MySQL and PostgreSQL. A proposal says to standardise on one engine, partly because developers keep applying advice from one engine's storage model to the other. How do you assess the proposal?Selectivity and cardinality
Define selectivity and cardinality for an index. Why might the planner ignore an index on a boolean column is_active where 95 percent of rows are true, and when would it use it?A query filters WHERE country = 'DE' AND city = 'Berlin'. The planner estimates 12 rows but 400,000 are returned, and it chooses a nested loop that runs for minutes. Why, and how do you fix the estimate in PostgreSQL?After a PostgreSQL major upgrade across 40 clusters, several services see plan regressions on skewed data (a few tenants own most rows). How do you manage the upgrade so plans do not regress unexpectedly next time?When indexes hurt writes
Why does every extra index slow down INSERT, UPDATE and DELETE, and how would you load 50 million rows into a new table quickly?A PostgreSQL table receives 20,000 updates per second to a last_seen_at column. It has 9 indexes, one of them on last_seen_at. WAL volume and replica lag are high. Explain the mechanics and what you would change.Across 400 databases, storage and IOPS costs grew 35 percent last year, and analysis shows a third of index space belongs to indexes with zero scans. How do you set up an organisation-wide approach to index cost without slowing teams down?GIN, GiST and BRIN indexes
Name a typical use for each PostgreSQL index type: GIN, GiST and BRIN. Write the index for a jsonb column attrs queried with attrs @> '{"color": "red"}'.An append-only sensor_readings table has 3 billion rows and a 90 GB B-tree on recorded_at. Queries ask for one hour or one day of data. Would BRIN be a better choice? What would you test, and what could make BRIN useless?Product teams want to add search across names, descriptions and tags to a PostgreSQL-backed catalogue of 80 million items. Some propose GIN full-text and trigram indexes; others want a separate search engine. How do you decide?Index maintenance and bloat
What does index bloat mean, and what commands rebuild an index in PostgreSQL and in MySQL InnoDB? Which ones block writes?A PostgreSQL index on a queue table is 25 GB while the table holds 200,000 live rows. Queries on it are getting slower. Explain the cause and how you fix it without downtime.Your team runs 150 PostgreSQL clusters. Bloat-related incidents (slow queries, disk full) happen monthly and each team handles them differently. What maintenance program would you put in place?Indexes on foreign keys
Deleting one row from customers takes 40 seconds and blocks other sessions.orders.customer_id references customers(id) ON DELETE CASCADE. What is the likely cause and fix?During a data cleanup, a batch job deletes 2 million rows from a parent table with five child tables, all with foreign keys. Replica lag reaches 20 minutes and lock waits cause errors in the application. How do you redo the cleanup safely?Unique indexes and constraints
What is the difference between a UNIQUE constraint and a unique index in PostgreSQL? How are NULLs treated, and how does PostgreSQL 15+ let you change that?Two instances of a Spring Boot service sometimes create duplicate accounts for the same email because they first SELECT, then INSERT. Show a correct approach using the database and handle the error in Java.Your company is splitting a monolith database into service-owned databases and some will be sharded. Global uniqueness of usernames and emails was guaranteed by unique indexes. What strategy do you set for global uniqueness and identity across the company?No question matches these choices.