Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a B-tree index in PostgreSQL?
A B-tree index is the default type of index in PostgreSQL. It organizes data in a balanced tree structure to allow fast searching, insertion, and deletion of rows based on the indexed column values.
Click to reveal answer
beginner
How does a B-tree index improve query performance?
It speeds up queries by quickly locating rows with specific values or ranges without scanning the entire table, using a balanced tree to minimize the number of disk reads.
Click to reveal answer
intermediate
Which types of queries benefit most from a B-tree index?
Queries that use equality (=), range comparisons (<, <=, >, >=), and ORDER BY clauses on the indexed columns benefit most from B-tree indexes.
Click to reveal answer
intermediate
Can a B-tree index be used for pattern matching with LIKE in PostgreSQL?
Yes, but only when the pattern does not start with a wildcard (e.g., LIKE 'abc%'). If the pattern starts with a wildcard (e.g., LIKE '%abc'), the B-tree index cannot be used efficiently.
Click to reveal answer
beginner
What happens to a B-tree index when data is inserted or deleted?
The B-tree index automatically updates to reflect the changes, maintaining its balanced structure to ensure fast access.
Click to reveal answer
What is the default index type in PostgreSQL?
AB-tree
BHash
CGIN
DGiST
✗ Incorrect
PostgreSQL uses B-tree as the default index type because it supports a wide range of queries efficiently.
Which query condition can a B-tree index NOT efficiently support?
AWHERE column = 5
BWHERE column LIKE '%abc'
CWHERE column > 10
DWHERE column LIKE 'abc%'
✗ Incorrect
B-tree indexes cannot efficiently support LIKE patterns starting with a wildcard such as '%abc'.
How does a B-tree index store data internally?
AIn a flat file
BIn a linked list
CIn a hash table
DIn a balanced tree structure
✗ Incorrect
B-tree indexes store data in a balanced tree structure to allow fast searching and updates.
Which of these operations causes a B-tree index to update?
ASELECT query
BCREATE TABLE
CINSERT
DDROP DATABASE
✗ Incorrect
Inserting data updates the B-tree index to include the new row's indexed values.
What kind of queries benefit from B-tree indexes?
ARange queries
BFull text search
CJSON queries
DSpatial queries
✗ Incorrect
B-tree indexes are optimized for range queries and equality comparisons.
Explain how a B-tree index works and why it is useful in PostgreSQL.
Think about how a tree helps find things quickly without checking every item.
You got /4 concepts.
Describe when a B-tree index can and cannot be used with the LIKE operator in PostgreSQL.
Consider how the pattern starts and how the index searches.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a B-tree index in PostgreSQL?
easy
A. To speed up searching and sorting operations
B. To store large binary objects
C. To manage user permissions
D. To backup the database automatically
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 A
Quick Check:
B-tree index = speed up search/sort [OK]
Hint: B-tree indexes speed up search and sort operations [OK]
Common Mistakes:
Confusing B-tree with storing large objects
Thinking indexes manage permissions
Assuming indexes handle backups
2. Which of the following is the correct syntax to create a B-tree index on the column username of table users?
easy
A. CREATE BTREE INDEX idx_username ON users (username);
B. CREATE INDEX idx_username ON users USING bitmap (username);
C. CREATE INDEX idx_username ON users (username) USING hash;
D. CREATE INDEX idx_username ON users USING btree (username);
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 D
Quick Check:
Correct syntax uses USING btree [OK]
Hint: Use 'USING btree' in CREATE INDEX for B-tree indexes [OK]
Common Mistakes:
Using incorrect index type like hash or bitmap
Wrong keyword order in CREATE INDEX
Omitting USING clause or misspelling it
3. Given a table 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?
medium
A. A sequential scan ignoring the index
B. A B-tree index scan to quickly find rows with price > 100
C. A hash index scan on price
D. A bitmap index scan on price
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 B
Quick Check:
Range query + order = B-tree index scan [OK]
Hint: Range queries with ORDER BY use B-tree index scans [OK]
Common Mistakes:
Assuming sequential scan always used
Confusing hash or bitmap index usage
Ignoring index benefits for range queries
4. You created a B-tree index on column email but notice queries filtering by LOWER(email) are slow. What is the likely problem?
medium
A. B-tree indexes only work on numeric columns
B. The index was created on the wrong table
C. B-tree indexes do not support functions like LOWER() by default
D. The database needs to be restarted to use the index
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 C
Quick Check:
Function in filter needs functional index [OK]
Hint: Use functional indexes for queries with functions like LOWER() [OK]
Common Mistakes:
Thinking B-tree only works on numbers
Assuming index applies automatically after restart
Mistaking wrong table for index issue
5. You want to enforce uniqueness on a column serial_number and speed up queries filtering by it. Which is the best approach using B-tree indexes?
hard
A. Create a UNIQUE B-tree index on serial_number
B. Create a non-unique B-tree index and a separate UNIQUE constraint
C. Create a hash index and a UNIQUE constraint
D. Create a UNIQUE constraint without an index
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 on serial_number -> Option A
Quick Check:
Unique B-tree index = uniqueness + speed [OK]
Hint: Use UNIQUE B-tree index to enforce uniqueness and speed queries [OK]
Common Mistakes:
Creating separate index and constraint unnecessarily