Learn about the different types of database indexes available in modern RDBMS and understand which type to use for different query patterns and data characteristics
Database indexes are arguably the most important performance optimization tool available to database developers. Like the index of a book helps you find content quickly without scanning every page, database indexes allow the database to locate data without examining every row in a table.
The impact of proper indexing can be dramatic:
However, not all indexes are created equal. Modern database systems offer multiple index types, each designed for specific types of data and query patterns. Choosing the wrong index type can waste storage space, slow down write operations, and fail to improve—or even worsen—query performance.
In this lesson, we'll explore the major index types available in PostgreSQL, MySQL, and other popular RDBMS. You'll learn how each index type works internally, when to use each one, and how to determine which index is best suited for your specific workload. By the end, you'll have a comprehensive understanding of the indexing toolkit available to you and how to leverage it for maximum performance.
Let's start by understanding how indexes work under the hood before diving into the specific types.
Before examining specific index types, let's understand the fundamentals of how database indexes work and why they're so critical for performance.
What Is a Database Index?
A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional storage space and slower writes. In essence, an index is a copy of selected data from a table, organized in a way that makes lookups extremely efficient.
The Indexing Analogy
Think of a database table without indexes like a book without an index or a table of contents. To find specific information, you'd need to scan through every page of the book—a sequential scan. With an index, you can quickly locate the pages containing relevant information, just as a database index helps the query engine find rows without scanning the entire table.
Core Index Mechanics
Database indexes typically work on these principles:
Key Index Properties
When discussing indexes, several properties matter:
The Cost of Indexes
While indexes dramatically improve read performance, they come with costs:
Index Storage Structure
Indexes are typically stored separately from the table data:
1TABLE DATA INDEX
2┌────────────────┐ ┌────────────────┐
3│ Row 1 │ │ Value -> Row 5 │
4│ Row 2 │ ◄─────────│ Value -> Row 2 │
5│ Row 3 │ │ Value -> Row 9 │
6│ Row 4 │ │ Value -> Row 1 │
7│ Row 5 │ ◄─────────│ Value -> Row 8 │
8└────────────────┘ └────────────────┘When Should Indexes Be Created?
Indexes are typically beneficial on:
Basic Index Types Across RDBMS
Most relational databases support these fundamental index types:
Let's examine each of these index types in detail to understand their strengths, limitations, and ideal use cases.
table with 1 million rows:
1SELECT * FROM orders WHERE customer_id = 12345;Without index:
With B-tree index on customer_id:
B-tree indexes are the default choice for most indexing needs due to their versatility and consistent performance. However, for specific workloads, other index types might provide better performance or space efficiency, which we'll explore next.
Your progress is saved in this browser, on this device only.