Skip to the page

SQL interview question · medium

Explain the concept of indexing in databases. What are the trade-offs between having many indexes and few indexes?

The short answerIndexing is a database optimization technique that enhances query performance by reducing the number of rows scanned.

The full answer

Indexing is a database optimization technique that enhances query performance by reducing the number of rows scanned. It creates a data structure (such as B-tree or Hash index) that enables efficient data retrieval. Instead of scanning an entire table, the database can use an index to find records faster.

How Indexing Works
Indexes function like a book’s table of contents, allowing the database engine to quickly locate relevant records without scanning all rows.

Example:
If a database frequently queries customer_id in the orders table, creating an index on this column improves performance:

CREATE INDEX idx_customer_id ON orders(customer_id);

This index allows the database to fetch records faster when filtering by customer_id.

Trade-offs Between Many Indexes and Few Indexes

AspectMany IndexesFew Indexes
Read PerformanceFaster queries due to efficient lookupsSlower queries as full table scans occur
Write PerformanceSlower writes (INSERT, UPDATE, DELETE) because indexes must be updatedFaster writes as fewer indexes need updating
Storage UsageRequires more disk spaceUses less disk space
Index MaintenanceNeeds frequent optimization (index rebuilds, defragmentation)Easier maintenance, fewer indexes to manage
Query OptimizationOptimized for frequent search queriesMay lead to performance issues in large datasets

Nuances to Consider for Indexing

  • Indexing Every Column is a Bad Idea

    • While indexes speed up searches, excessive indexes increase overhead on write operations.
    • Always index columns based on query patterns rather than blindly indexing every field.
  • Choose the Right Type of Index

    • B-Tree Index: Suitable for most queries (WHERE, ORDER BY, GROUP BY).
    • Hash Index: Faster for exact matches but not suitable for range queries.
    • Full-Text Index: Ideal for searching textual content.
  • Use Composite Indexes for Multi-Column Searches

    • Instead of creating separate indexes on first_name and last_name, a composite index improves queries filtering both columns:
      CREATE INDEX idx_name ON users(first_name, last_name);
      
  • Monitor Index Usage

    • Use EXPLAIN ANALYZE or pg_stat_user_indexes (PostgreSQL) to check if indexes are being used efficiently.
  • Rebuild Indexes Regularly

    • Over time, indexes become fragmented, affecting performance.
    • Periodic index rebuilding (REINDEX in PostgreSQL) ensures efficiency.

Best Practices for Indexing

  • Index only the columns frequently used in searches or joins.
  • Avoid indexing columns with high update frequency, as it slows down write operations.
  • Use Partial Indexes for indexing only a subset of data instead of the entire table.
  • Periodically analyze and drop unused indexes to free up storage.

Tecyfy Takeaway
Indexes are powerful for improving query performance, but they come with trade-offs. While they enhance read speed, excessive indexing slows write performance and consumes storage. A balanced approach—indexing only necessary columns, monitoring index usage, and optimizing periodically—ensures an efficient database system.

Last updated 1 February 2025