B-tree index (default) behavior in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how fast a B-tree index helps find data in a database as the data grows.
How does the time to search change when we add more rows?
Analyze the time complexity of the following SQL query using a B-tree index.
-- Assume a table 'users' with a B-tree index on 'email'
SELECT * FROM users WHERE email = 'example@example.com';
This query looks up a user by email using the B-tree index to speed up the search.
In a B-tree search, the main repeating operation is moving down tree levels.
- Primary operation: Comparing the search key with node keys at each tree level.
- How many times: Once per tree level, which grows slowly as data grows.
As the number of rows grows, the tree gets taller but only slowly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 2 comparisons |
| 100 | About 3 comparisons |
| 1000 | About 4 comparisons |
Pattern observation: The number of steps grows very slowly, roughly adding one step for every tenfold increase in data.
Time Complexity: O(log n)
This means the search time grows slowly as data grows, making lookups efficient even for large tables.
[X] Wrong: "Searching with a B-tree index takes the same time as scanning every row one by one."
[OK] Correct: A B-tree index narrows down the search quickly by jumping through tree levels, so it does not check every row.
Knowing how B-tree indexes scale helps you explain why databases stay fast as data grows, a key skill for working with real systems.
"What if the index was a hash index instead? How would the time complexity change for exact match searches?"
Practice
Solution
Step 1: Understand the role of indexes
Indexes help databases find data faster without scanning the entire table.Step 2: Identify B-tree index function
B-tree indexes organize data to speed up searching and sorting efficiently.Final Answer:
To speed up searching and sorting operations -> Option AQuick Check:
B-tree index = speed up search/sort [OK]
- Confusing B-tree with storing large objects
- Thinking indexes manage permissions
- Assuming indexes handle backups
username of table users?Solution
Step 1: Recall correct CREATE INDEX syntax
The syntax is CREATE INDEX index_name ON table_name USING index_type (column);Step 2: Identify correct index type and syntax
B-tree is default and specified as USING btree; CREATE INDEX idx_username ON users USING btree (username); matches this exactly.Final Answer:
CREATE INDEX idx_username ON users USING btree (username); -> Option DQuick Check:
Correct syntax uses USING btree [OK]
- Using incorrect index type like hash or bitmap
- Wrong keyword order in CREATE INDEX
- Omitting USING clause or misspelling it
products(id SERIAL PRIMARY KEY, price NUMERIC) with a B-tree index on price, what will the query SELECT * FROM products WHERE price > 100 ORDER BY price; most likely use?Solution
Step 1: Understand query conditions and index type
The query filters with price > 100 and orders by price; B-tree indexes support range queries and sorting.Step 2: Identify index usage
PostgreSQL will use the B-tree index to efficiently find and order matching rows.Final Answer:
A B-tree index scan to quickly find rows with price > 100 -> Option BQuick Check:
Range query + order = B-tree index scan [OK]
- Assuming sequential scan always used
- Confusing hash or bitmap index usage
- Ignoring index benefits for range queries
email but notice queries filtering by LOWER(email) are slow. What is the likely problem?Solution
Step 1: Understand function usage in WHERE clause
Using LOWER(email) means the query filters on a transformed value, not the raw column.Step 2: Recognize index limitations
Regular B-tree indexes do not support functions unless a functional index is created.Final Answer:
B-tree indexes do not support functions like LOWER() by default -> Option CQuick Check:
Function in filter needs functional index [OK]
- Thinking B-tree only works on numbers
- Assuming index applies automatically after restart
- Mistaking wrong table for index issue
serial_number and speed up queries filtering by it. Which is the best approach using B-tree indexes?Solution
Step 1: Understand uniqueness enforcement
PostgreSQL enforces UNIQUE constraints using unique indexes, usually B-tree by default.Step 2: Combine uniqueness and performance
Creating a UNIQUE B-tree index both enforces uniqueness and speeds up lookups on that column.Final Answer:
Create a UNIQUE B-tree index onserial_number-> Option AQuick Check:
Unique B-tree index = uniqueness + speed [OK]
- Creating separate index and constraint unnecessarily
- Using hash index which doesn't enforce uniqueness
- Assuming UNIQUE constraint works without an index
