Intermediate Topic 20 of 51 Databases
Indexes
Auxiliary structures speeding up lookups.
How it works
An index is an auxiliary data structure, most often a B-tree, that lets the database find rows without scanning the entire table, much like a book index. Indexes can be on one or several columns, unique, partial, covering with included columns, or special types such as hash, GIN, GiST and full-text indexes. They speed up lookups, joins, sorting and constraints, but each index uses storage and must be updated on every write.
Step-by-step walkthrough
- Without an index the database scans every row.
- An index is a sorted structure like a B-tree.
- The query searches the index in O(log n) steps.
- It jumps straight to the matching rows.
- Indexes speed reads but slow writes and use space.
Where it is used
Indexing foreign keys, columns used in WHERE and JOIN, and sort columns turns slow table scans into fast lookups for web apps and reports.
Common pitfall
Too many indexes slow inserts and updates and waste space, while missing ones cause full scans. Column order in a composite index matters, since it supports leftmost prefixes only. Functions on indexed columns or low-selectivity columns may make them unusable or useless. Verify with execution plans and drop unused indexes.
Where it fits in the course
Indexes is topic 20 of 51 in Databases, rated Intermediate. It follows Views. The next topic is Normalization.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Databases quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Databases quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Indexes?
Where do people use Indexes?
What is the most common mistake with Indexes?
What should I learn before this topic?
How can I practice it?
Is Indexes hard to learn?
How long does it take to learn Indexes?
Which topics are related to Indexes?
What should I study after Indexes?
Related topics
- Views (Databases)
- Normalization (Databases)
- Subqueries (Databases)
- Normal Forms (Databases)