Database Indexing Deeply: Types, Trade-offs, and Write Amplification
Learn database indexing, including B-Tree, Hash, Composite, Unique, Partial, and Covering indexes, their performance trade-offs, and write amplification.

Introduction
As a database grows, finding the right data can become slower, especially when a table contains thousands or even millions of records. This is where database indexing comes in. An index helps the database find the required data faster without having to scan every row in the table.
But adding indexes is not always a free performance boost. Indexes take extra storage and also need to be updated whenever data is inserted, updated, or deleted.
Quick Answer
We can create indexes on columns that are frequently used in queries, such as where, join, and order by. This helps the database find the required data faster without scanning the entire table.
What Is Database Indexing?
Database indexing is a technique used to make data retrieval faster. Instead of scanning every row in a table, the database can use an index to quickly locate the required records.
However, indexes also come with costs. They require additional storage and need to be maintained whenever the underlying data changes.
The Problem
If you are not Indexing the field of the Database then you might need to face below problem :
- Slower Read Queries: The database runs a full table scan, meaning it checks every single row from top to bottom to find the requested data.
- High Resource Usage: Searches consume much more processor (CPU) and memory power.
- Poor Scalability: As your table grows from thousands to millions of rows, query times jump from milliseconds to several seconds or minutes.
The Solution
The solution is to create appropriate indexes on columns that are frequently used in WHERE, JOIN, ORDER BY, or GROUP BY queries. Indexes help the database locate the required records efficiently instead of scanning the entire table. This can significantly improve query response time and reduce unnecessary database load. However, indexes should be created carefully because they require additional storage and can increase write overhead.
Types of Database Indexes
Different types of indexes are designed for different query patterns. Choosing the right index depends on how the application reads and filters data.
B-Tree Index
B-Tree is one of the most commonly used index types in relational databases. It works well for equality searches, range queries, and sorting.
under B-Tree Index, you should have:
CREATE INDEX idx_users_email
ON users(email);B-Tree indexes are also useful for range conditions such as:
SELECT *
FROM users
WHERE age > 30 ;Hash Index
A Hash index is designed mainly for exact-match lookups. It uses a hash structure to locate values quickly.
For example:
SELECT *
FROM users
WHERE email = 'user@example.com';Hash indexes can be useful when queries primarily use equality conditions. However, they are generally not suitable for range queries such as:
WHERE age > 30Composite Index
A Composite index is an index created on multiple columns.
For Example :
CREATE INDEX idx_users_country_city
ON users(country, city);This can help queries that filter using both columns:
WHERE country = 'India'
AND city = 'Surat'The order of columns matters in a composite index. An index on (country , city) is not automatically equivalent to an index on (city, country).
Unique Index
A Unique index prevents duplicate values in the indexed column or combination of columns while also providing an index for lookups.
For Example :
CREATE UNIQUE INDEX idx_users_email
ON users(email);This ensures that two users cannot have the same email address. Unique indexes are useful for columns such as Email addresses, Usernames, Employee IDs, Other values that must be unique
Partial / Filtered Index
A Partial or Filtered index stores index entries only for rows that satisfy a specific condition.
For example, an application may frequently search only active users:
CREATE INDEX idx_active_users
ON users(email)
WHERE status = 'active';This allows the index to focus on the rows that are relevant to the query.
Covering Index
A Covering index contains all the columns required by a particular query. In some cases, this allows the database to answer the query directly from the index without fetching the corresponding table rows.
For Example,
CREATE INDEX idx_users_email_name
ON users(email, name);The following query only needs email for filtering and name for the result:
SELECT name
FROM users
WHERE email = 'user@example.com';Because the required columns are available in the index, the database may be able to avoid accessing the full table row. Whether this happens depends on the database engine and the query execution plan.
Prerequisites
- SQL and relational databases
- Tables, rows, and columns
- Basic SELECT, INSERT, UPDATE, and DELETE queries
- WHERE, JOIN and ORDER BY clauses
- Basic understanding of database performance
Step-by-Step Implementation
Step 1: Create Table
Start with a simple users table containing a large number of records.
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(255),
age INT,
country VARCHAR(100)
);Step 2: Run Query Without Index
Generally, we may fetch users by their email address. Without a suitable index, the database may need to scan many rows in the users table to find the matching record.
SELECT *
FROM users
WHERE email = 'john@example.com';Step 3: Check the Query Plan
Use EXPLAIN Keyword for the lookup How Database Query is Executed by the Query Optimizer in the Database.
EXPLAIN
SELECT *
FROM users
WHERE email = 'john@example.com';That EXPLAIN Keywords Shows how many rows are needed to lookup by query for a match that results without indexing.
Step 4: Create an Index
Create indexes on columns that are frequently used in queries. This can make data retrieval more efficient.CREATE INDEX idx_users_email
ON users(email);Step 5: Again Run Explain
Compare the execution plan before and after creating the index. The goal is to determine whether the database can use the index and whether the query becomes more efficient.
EXPLAIN
SELECT *
FROM users
WHERE email = 'john@example.com';Step 6: Understand the Trade-Off
Indexing can improve read performance, but it is not free. Indexes require additional storage and must be maintained when data is inserted, updated, or deleted.

Step 7: Understand Write Amplification
For example, if a table has several indexes, inserting one row may require the database to update the table and each relevant index. Similarly, updating or deleting indexed data can require changes to multiple index structures. As the number of indexes increases, the amount of work required for each write can also increase.
INSERT INTO users (id, name, email, age, country)
VALUES (1001, 'John', 'john@example.com', 25, 'India');With multiple indexes:
Step 8: Measure Before and After
Finally, compare data given by the Explain for the Query Execution, As you can Expect result like this,
| Comparison | With Indexing | Without Indexing |
|---|---|---|
| Query execution time | Usually lower | Usually higher |
| Rows examined | Often fewer | Often more |
| Query Execution Plan | May use an index | May use an a table / full scan |
| Additional Index Storage | More | less |
| Insert, update and delete performance | Usually higher | Usually lower |
The goal is to find a balance between faster reads and the additional cost of maintaining indexes.
Common Problems / Errors
| PROBLEM | SOLUTION |
|---|---|
| Index is not being used | The database chooses a table scan instead of using the created index. |
| Too many indexes | Extra indexes increase storage usage and index-maintenance overhead. |
| Wrong index type | The selected index does not match the query pattern, resulting in little or no performance improvement. |
| Wrong composite index order | The database may not be able to efficiently use the index for certain queries. |
| Queries are still slow | The index may not address the actual bottleneck, or the query may need optimization. |
| Slower INSERT, UPDATE, and DELETE | Data changes require additional index maintenance. |
| High write amplification | A single data change can cause updates to multiple index structures. |
| Duplicate or unused indexes | Redundant indexes consume storage and increase unnecessary write overhead. |
| Unexpected query plan | The optimizer may choose a different access path based on statistics, data distribution, or estimated cost. |
Best Practices
- Index Based on Actual Queries – Create indexes for frequently used queries instead of indexing every column.
- Choose the Right Index Type – Select B-Tree, Hash, Composite, or other indexes based on the query pattern.
- Use EXPLAIN – Check the query execution plan to confirm whether the index is actually being used.
- Avoid Unnecessary Indexes – Too many or duplicate indexes increase storage and write-maintenance costs.
- Balance Read and Write Performance – Improve read performance while considering the additional cost and write amplification caused by indexes.
Performance and Security Considerations
- Index Only When Necessary – Avoid excessive indexes because they increase storage usage and can slow down INSERT, UPDATE, and DELETE operations.
- Monitor Write Amplification – Multiple indexes can increase the amount of work required for every data change, especially in write-heavy systems.
- Protect Sensitive Data – Avoid unnecessarily indexing sensitive columns, as indexes may contain copies or representations of indexed values.
- Review Query Execution Plans – Use EXPLAIN to identify inefficient queries and ensure indexes are being used effectively.
- Balance Performance and Storage – Choose indexes that provide meaningful query improvements without consuming excessive storage or system resources.
When Should You Use It?
- When frequently searching data using WHERE.
- When tables contain a large number of records.
- When columns are frequently used in JOIN,ORDER BY, or GROUP BY.
- When queries are slow due to full table scans.
- When the read-performance improvement justifies the additional storage and write overhead.
When Should You Avoid or Reconsider an Index?
- When the table is very small and a table scan is already inexpensive.
- When a column is rarely used in queries.
- When an existing index already covers the required query pattern.
- When the index provides little performance benefit but adds significant write overhead.
- In write-heavy workloads where additional index maintenance outweighs the read-performance benefit.
FAQ
1. What is database indexing?
Database indexing is a technique that helps the database find and retrieve data faster without scanning the entire table.
2. Does indexing always improve performance?
No. Indexes can improve read performance, but unnecessary indexes can increase storage usage and slow down write operations.
3. What are the common types of database indexes?
Common types include B-Tree, Hash, Composite, Unique, Partial/Filtered, and Covering indexes.
4. What is write amplification in database indexing?
Write amplification occurs when a single INSERT, UPDATE, or DELETE requires additional work to maintain one or more indexes.
5. Can too many indexes hurt database performance?
Yes. Too many indexes consume additional storage and increase the maintenance cost of write operations.
6. How do I check whether an index is being used?
Use the database's query-plan tools, such as EXPLAIN or EXPLAIN ANALYZE , to check how a query is executed.
7. Should every column have an index?
No. Indexes should be created based on actual query patterns and workload requirements.
8. How do I choose the right index?
Choose the index type based on how the data is queried, such as equality searches, range queries, sorting, or multiple-column filtering.
9. Do indexes affect INSERT, UPDATE and DELETE?
Yes. The database may need to update the relevant index structures whenever indexed data changes, increasing write overhead.
10. What is the main goal of database indexing?
The goal is to achieve faster data retrieval while balancing storage, maintenance costs, and write amplification.
11. What happens when an indexed column is updated?
When an indexed column changes, the database may need to update the corresponding index structure, which adds additional work to the update operation.
12. Does an index increase database storage?
Yes. Indexes require additional storage because the database maintains index structures separately from the table data.
Conclusion
Database indexing is an important technique for improving query performance, especially when working with large tables. Different index types are designed for different query patterns, so choosing the right type is essential.
However, indexes come with trade-offs. They require additional storage and maintenance, and too many indexes can increase write amplification, making INSERT, UPDATE and DELETE operations more expensive.
The goal is not to create as many indexes as possible, but to create the right indexes for the right queries while maintaining a balance between read performance, write performance, storage, and overall database efficiency.

Aarav Sharma
Lead Software Engineer
Aarav leads product engineering at Matlab Infotech, where he has shipped mobile and web platforms across healthcare, fintech, and SaaS. He writes about pragmatic engineering and shipping fast without cutting corners.