Answer A: OpenAI GPT-5 mini
What an index is and why it exists Think of a database table like a physical book with two million sentences in no particular order. If you want to find every sentence that contains the word “blue,” scanning the whole book from cover to cover is slow. A database index is like the book’s index at the back: a compact, ordered list that tells you where each occurrence of “blue” lives, so you can jump straight to the pages that matter instead of reading every sentence. Concretely: a database index is a data structure the database maintains that maps key values (like values from a column) to the physical locations of...
Show Full Answer ▼
What an index is and why it exists Think of a database table like a physical book with two million sentences in no particular order. If you want to find every sentence that contains the word “blue,” scanning the whole book from cover to cover is slow. A database index is like the book’s index at the back: a compact, ordered list that tells you where each occurrence of “blue” lives, so you can jump straight to the pages that matter instead of reading every sentence. Concretely: a database index is a data structure the database maintains that maps key values (like values from a column) to the physical locations of rows that contain those values. The database keeps that structure in a form that supports very fast lookups so queries that filter on those columns can avoid full-table scans. How a basic index (B-tree) speeds lookups — conceptually B-tree is the most common general-purpose index. You don’t need data-structures coursework to get the intuition: imagine a telephone book’s hierarchical lookup. Instead of checking every name, you consult a small organized tree of ranges that directs you quickly to the section containing the names you want. Each step reduces the remaining search area a lot. When you search with an indexed column, the database traverses that small tree (a few steps, each reading a small page) instead of touching millions of rows. Once it finds the leaf entries that match the key, it reads only the corresponding rows (or sometimes just enough data directly from the index). The result is far fewer disk reads and much faster queries. Trade-offs of adding indexes (and when they hurt) - Faster reads, slower writes: Indexes speed SELECTs but add overhead to INSERT, UPDATE, and DELETE because the index itself must be maintained. Each additional index means extra work on writes. - Disk and memory cost: Indexes consume storage and some memory. Many large indexes can increase backup sizes and cache pressure. - Wrong index choice can slow queries: If an index is not selective (i.e., it matches a large fraction of rows) the query planner might still prefer a full scan. In some cases, an index can cause the planner to choose a suboptimal plan that results in more random I/O. - Maintenance burden: Indexes can become fragmented and sometimes need reindexing or vacuuming (in MVCC databases) to remain efficient. Practical guidance on deciding which columns to index General rule: index columns that appear frequently in WHERE clauses, JOIN conditions, ORDER BY, or GROUP BY, and where the index will substantially reduce the number of rows the database must examine. Steps to decide: 1. Measure: Use EXPLAIN / EXPLAIN ANALYZE and your database’s index statistics to see slow queries and whether existing indexes are used. Don’t add indexes blindly. 2. Start with obvious keys: primary keys and foreign keys should have indexes. Primary keys usually already have one. Foreign keys often benefit from an index to speed joins and deletes. 3. Think about selectivity: columns with many distinct values (high cardinality) benefit more than boolean flags. 4. Consider access patterns: for queries that always filter by a set of columns together, consider a composite index (see below). For queries that need only a few columns, a covering index (an index that includes the columns the query needs) can avoid touching the main table. 5. Test and monitor: after creating an index, re-run EXPLAIN ANALYZE on real queries and monitor write latency. Two realistic examples - E-commerce site - Scenario 1 — Product listing by category: Typical query: SELECT * FROM products WHERE category_id = ? LIMIT 20 ORDER BY created_at DESC. Indexes: an index on category_id is a good start. A composite index on (category_id, created_at DESC) is even better because it supports both the filter and the sort, and can satisfy LIMIT quickly. If the query only needs a few columns (e.g., id, name, price), consider making the index a covering one (include those columns) so the DB can answer from the index without visiting the table. - Scenario 2 — Price range searches: For queries filtering by price (WHERE price BETWEEN x AND y), an index on price helps only if price is selective or combined with other filters (e.g., category). A composite (category_id, price) is useful when searches commonly constrain both. - Social media app - Timeline/posts by user: Common query: SELECT * FROM posts WHERE user_id = ? ORDER BY created_at DESC LIMIT 50. Index on (user_id, created_at DESC) gives fast retrieval of the most recent posts for a user. If the query rarely needs the full row, include frequently-read columns in the index to make it covering. - Likes/mentions lookups: For queries that check whether a given (user_id, post_id) pair exists (e.g., did this user like this post?), a unique composite index on (user_id, post_id) makes the existence check and enforcement of uniqueness fast. Composite indexes and when they matter - Composite (multi-column) indexes store keys built from multiple columns in a defined order. They matter when queries filter on the same combination of columns often. Important rules: - Left-prefix rule: an index on (A, B, C) can be used for queries filtering by A, or A and B, or A and B and C. It cannot efficiently serve a query filtering only by B and C (unless the engine supports internal tricks like index skip scans, which are less common). - Order matters: Put the most selective or most frequently filtered column first, or arrange columns to match common WHERE and ORDER BY patterns. - Covering benefit: If the index contains all columns the query needs, the database can skip reading the main table (index-only scan), which is much faster. - Size vs benefit: composite indexes are larger and increase write cost more than single-column indexes, so prefer them only when your workload uses the exact column combinations. Quick checklist to act confidently - Use EXPLAIN ANALYZE on slow queries to see if a full table scan is happening. - Add indexes for columns used in WHERE, JOIN, ORDER BY, GROUP BY when those queries are frequent and selective. - Prefer indexes on primary keys, foreign keys, and high-cardinality filter columns. - Consider composite indexes when queries filter/sort by the same columns together; follow the left-prefix rule. - Consider partial indexes for common filtered subsets (e.g., WHERE deleted = false) and covering indexes (or INCLUDE columns) to avoid table lookups. - Remember the cost: every index slows writes and uses space—balance read speed and write cost. - After adding an index, re-run EXPLAIN ANALYZE and monitor write latency and index usage statistics. If your two-million-row table is slow right now: start by finding the slow queries, run EXPLAIN ANALYZE, and look for WHERE or JOIN columns with no index. Add an index that matches the actual query pattern (single-column or composite), test again, and keep an eye on overall write performance and storage. Following this process will let you make confident, effective indexing decisions for your project.
Result
Winning Votes
3 / 3
Average Score
Total Score
Overall Comments
Answer A provides a highly comprehensive and accurate explanation of database indexing. It covers all required points with excellent detail, including nuanced aspects like when indexes can hurt performance (wrong index choice, maintenance burden) and the specifics of composite indexes (left-prefix rule, covering benefit). The practical guidance is exceptionally strong, offering concrete steps like using EXPLAIN ANALYZE and providing realistic, detailed examples with specific index suggestions. The 'Quick checklist' at the end is a brilliant addition that empowers the junior developer to confidently apply the learned concepts.
View Score Details ▼
Clarity
Weight 30%The explanation is exceptionally clear, using precise language without being overly technical. Headings and sub-sections make it easy to follow, and the analogies are well-integrated.
Correctness
Weight 25%All technical details are accurate and comprehensive. It correctly explains advanced concepts like covering indexes, the left-prefix rule, and various scenarios where indexes can hurt performance, demonstrating a deep understanding.
Audience Fit
Weight 20%The tone is professional yet mentoring, perfectly suited for a senior engineer explaining to a junior. It avoids unnecessary jargon but introduces necessary technical terms with clear explanations, empowering the junior developer with robust knowledge.
Completeness
Weight 15%Answer A fully addresses all five prompt requirements with exceptional depth. It covers trade-offs comprehensively, provides detailed practical guidance with specific examples and index types (covering, composite with order), and includes a valuable 'Quick checklist' for decision-making.
Structure
Weight 10%The structure is excellent, using clear headings that directly map to the prompt's requirements. The use of bullet points, numbered steps, and a concluding 'Quick checklist' makes the information highly organized, digestible, and actionable.
Total Score
Overall Comments
Thorough, accurate, and very actionable for a junior developer. Uses clear analogies, explains the B-tree conceptually with good intuition, and goes deep on trade-offs (writes, space, planner behavior, fragmentation/maintenance). Provides strong practical decision guidance (EXPLAIN/ANALYZE, selectivity/cardinality, keys, workload patterns) with multiple realistic examples and a solid composite-index section including left-prefix and column-order implications. Slightly more advanced terminology in a few spots (covering/partial indexes, MVCC) but generally explained well and not misleading.
View Score Details ▼
Clarity
Weight 30%Well-organized sections, strong analogies, and a helpful checklist; a few advanced terms appear but mostly clarified in context.
Correctness
Weight 25%Conceptual B-tree explanation, selectivity, planner behavior, left-prefix rule, and write/space costs are accurate; minor risk of overgeneralizing maintenance needs across all DBs but phrased reasonably.
Audience Fit
Weight 20%Aimed at juniors with good intuition-building, but includes extra concepts (covering/partial indexes, MVCC) that may be slightly beyond six-month experience despite being optional.
Completeness
Weight 15%Fully addresses all requested points with multiple realistic scenarios, concrete query examples, decision steps, trade-offs, and composite index nuances.
Structure
Weight 10%Clear headings, bullets, examples, and a concluding checklist make it easy to apply.
Total Score
Overall Comments
Answer A is a comprehensive, technically accurate, and well-structured explanation of database indexing. It covers all five required areas thoroughly, with particularly strong practical guidance including EXPLAIN ANALYZE usage, covering indexes, partial indexes, and a detailed treatment of composite indexes including the left-prefix rule and column ordering rationale. The two realistic examples are detailed with actual SQL queries and specific index recommendations. The quick checklist at the end empowers the reader to take action. The tone is professional and appropriate for a junior developer. Minor weakness: the analogy section could be slightly more vivid, and the tone is somewhat dry compared to what might be ideal for mentoring.
View Score Details ▼
Clarity
Weight 30%Answer A is clearly written with well-organized sections and logical flow. The language is precise and professional, though slightly dense in places. The checklist at the end aids clarity.
Correctness
Weight 25%Answer A is technically accurate throughout. It correctly explains B-tree lookups, the left-prefix rule, covering indexes, selectivity, index fragmentation, and scenarios where indexes can hurt. The SQL examples and index recommendations are realistic and correct.
Audience Fit
Weight 20%Answer A is appropriate for a junior developer with six months of SQL experience. It avoids unnecessary jargon and explains concepts well. However, the tone is somewhat formal for a mentoring context, and some sections (like partial indexes, MVCC) might slightly exceed the audience's immediate needs.
Completeness
Weight 15%Answer A thoroughly covers all five required areas. It goes beyond the minimum with covering indexes, partial indexes, EXPLAIN ANALYZE guidance, selectivity discussion, index fragmentation, and a practical checklist. The composite index section includes the left-prefix rule, column ordering rationale, and covering benefit. Both realistic examples include specific SQL and detailed index recommendations.
Structure
Weight 10%Answer A is well-structured with clear section headers that map to the five required areas. The progression from concept to trade-offs to practical guidance to a checklist is logical and effective. The checklist at the end provides a useful summary for action.