

B
Discuss the purpose of using indexes in a database. How do they enhance query performance, and what are the potential trade-offs associated with their use? Provide examples of scenarios where indexing is beneficial and where it may be counterproductive.
Focus on the mechanics of indexing, including types of indexes, their benefits in query optimization, and situations where they may introduce overhead. Aim for a comprehensive understanding of how indexes work and their implications on database performance.
Indexes are data structures that improve the speed of data retrieval operations on a database table at the cost of additional space and maintenance overhead.
CREATE INDEX idx_customer_name ON customers(name);
Common types include B-tree indexes for general use, hash indexes for equality comparisons, and full-text indexes for searching text data.
CREATE FULLTEXT INDEX idx_fulltext_description ON products(description);
Indexes significantly reduce the amount of data the database engine needs to scan, leading to faster query performance, especially for large datasets.
SELECT * FROM orders WHERE customer_id = 123;
While indexes speed up read operations, they can slow down write operations (INSERT, UPDATE, DELETE) due to the overhead of maintaining the index.
INSERT INTO products(name, price) VALUES ('New Product', 19.99);