Fatskills
Practice. Master. Repeat.
Study Guide: Database-Systems Indexes Indexes How They Speed Up Queries When to Avoid
Source: https://www.fatskills.com/databases/chapter/database-systems-indexes-indexes-how-they-speed-up-queries-when-to-avoid

Database-Systems Indexes Indexes How They Speed Up Queries When to Avoid

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is and Why It Matters

Indexes are crucial structures in database systems that significantly speed up data retrieval. They work by creating a data structure that allows for efficient querying, similar to how an index in a book helps you find information quickly. Understanding indexes is vital for database performance optimization. Poorly designed indexes can lead to slow queries and increased storage costs. For instance, an e-commerce site with inefficient indexing might experience slow search results, leading to frustrated users and lost sales.

Core Knowledge (What You Must Internalize)

  • Index: A database object that improves the speed of data retrieval operations on a table at the cost of additional writes and storage space. (Why this matters: It's the foundation of efficient querying.)
  • B-tree Index: The most common type of index, which organizes data in a balanced tree structure. (Why this matters: It's widely used and efficient for range queries.)
  • Hash Index: An index type that uses a hash table to map keys to values. (Why this matters: It's efficient for equality searches but not for range queries.)
  • Clustered Index: An index that determines the physical order of data in a table. (Why this matters: Only one clustered index per table is allowed, affecting how data is stored.)
  • Non-clustered Index: An index that does not alter the physical order of the table. (Why this matters: Multiple non-clustered indexes can exist per table, providing flexibility.)
  • Composite Index: An index that includes multiple columns. (Why this matters: Useful for queries that filter on multiple columns.)
  • Index Selectivity: The uniqueness of values in an indexed column. (Why this matters: High selectivity means the index is more effective.)

Step‑by‑Step Deep Dive

  1. Understand the Basics of Indexes
  2. An index is a data structure that improves the speed of data retrieval.
  3. Indexes are created on columns of a table.
  4. Example: Creating an index on the customer_id column in a sales table.
    ⚠️ Common pitfall: Creating too many indexes can slow down insert and update operations.

  5. Choose the Right Type of Index

  6. B-tree Index: Good for range queries.
    • Example: Finding all sales between two dates.
  7. Hash Index: Good for exact match queries.
    • Example: Finding a specific customer by ID.
  8. Clustered Index: Determines the physical order of data.
    • Example: Ordering sales data by date.
  9. Non-clustered Index: Does not alter the physical order.


    • Example: Indexing multiple columns for different query patterns.
  10. Create a Composite Index

  11. A composite index includes multiple columns.
  12. Useful for queries that filter on multiple columns.
  13. Example: Indexing last_name and first_name for efficient name searches.
    ⚠️ Common pitfall: The order of columns in a composite index matters.

  14. Evaluate Index Selectivity

  15. High selectivity means the index is more effective.
  16. Example: Indexing a customer_id column with unique values is highly selective.
  17. Low selectivity columns (e.g., gender) are less effective for indexing.

  18. Monitor and Maintain Indexes

  19. Indexes need maintenance to remain effective.
  20. Regularly rebuild or reorganize indexes to keep them optimized.
  21. Example: Rebuilding an index after a large data insert.
    ⚠️ Common pitfall: Neglecting index maintenance can lead to performance degradation.

How Experts Think About This Topic

Experts view indexes as a trade-off between query performance and storage/write costs. They focus on the most frequently queried columns and balance the number of indexes to avoid excessive overhead. Instead of creating indexes for every column, they prioritize based on query patterns and selectivity.

Common Mistakes (Even Smart People Make)

  1. The mistake: Creating indexes on every column.
  2. Why it's wrong: Increases storage and slows down write operations.
  3. How to avoid: Prioritize indexes based on query patterns and selectivity.
  4. Exam trap: Questions that ask for the optimal number of indexes.

  5. The mistake: Ignoring index maintenance.

  6. Why it's wrong: Leads to fragmented and inefficient indexes.
  7. How to avoid: Regularly rebuild or reorganize indexes.
  8. Exam trap: Scenarios that require identifying maintenance needs.

  9. The mistake: Using hash indexes for range queries.

  10. Why it's wrong: Hash indexes are inefficient for range queries.
  11. How to avoid: Use B-tree indexes for range queries.
  12. Exam trap: Questions that mix index types and query patterns.

  13. The mistake: Creating a clustered index on a non-unique column.

  14. Why it's wrong: Can lead to inefficient data storage and retrieval.
  15. How to avoid: Use unique columns for clustered indexes.
  16. Exam trap: Scenarios that require choosing the right column for a clustered index.

Practice with Real Scenarios

Scenario: An e-commerce database with a sales table. The table has columns: sale_id, customer_id, product_id, sale_date, and amount.

Question 1: Which type of index should be used for efficient retrieval of sales data by sale_date range?

Solution:
1. Identify the query pattern: Range query on sale_date.
2. Choose the appropriate index type: B-tree index.
3. Create the index: CREATE INDEX idx_sale_date ON sales(sale_date);

Answer: B-tree index.

Why it works: B-tree indexes are efficient for range queries.

Question 2: Should a clustered index be created on the customer_id column?

Solution:
1. Check if customer_id is unique: No, it is not unique.
2. Decide on the index type: Non-clustered index.
3. Create the index: CREATE INDEX idx_customer_id ON sales(customer_id);

Answer: No, use a non-clustered index.

Why it works: Clustered indexes should be on unique columns for efficient storage.

Quick Reference Card

  • Core rule: Indexes speed up queries but add storage and write costs.
  • Key formula: CREATE INDEX index_name ON table_name(column_name);
  • Three most critical facts:
  • B-tree indexes are best for range queries.
  • Hash indexes are best for exact match queries.
  • High selectivity columns are best for indexing.
  • One dangerous pitfall: Creating too many indexes.
  • Mnemonic: "B for range, Hash for match, Selective for speed."

If You're Stuck (Exam or Real Life)

  • What to check first: Verify the query patterns and column selectivity.
  • How to reason from first principles: Balance query performance with storage and write costs.
  • When to use estimation: Estimate the impact of indexes on storage and performance.
  • Where to find the answer: Consult database documentation and performance monitoring tools.

Related Topics

  • Query Optimization: Understanding how queries are executed and optimized.
  • Database Normalization: Structuring data to minimize redundancy and improve integrity.


ADVERTISEMENT