Partial indexes with WHERE clause in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how using a partial index affects the speed of database queries.
Specifically, how the time to find data changes as the table grows when the index only covers some rows.
Analyze the time complexity of this partial index creation and usage.
CREATE INDEX idx_active_users ON users (last_login) WHERE active = true;
SELECT * FROM users WHERE active = true AND last_login > now() - interval '7 days';
This code creates an index only on users who are active, then queries recent active users.
Look at what repeats when the query runs.
- Primary operation: Searching the partial index for matching rows.
- How many times: Depends on the number of active users, not all users.
The query only searches the smaller set of active users, so as total users grow, work grows slower.
| Input Size (total users) | Approx. Operations (active users) |
|---|---|
| 10 | 5 (if half active) |
| 100 | 50 |
| 1000 | 500 |
Pattern observation: Work grows with the number of active users, not total users.
Time Complexity: O(log m + k)
This means the query time grows with the size of the indexed subset (active users), not the whole table, where m is the number of indexed rows and k is the number of rows returned.
[X] Wrong: "The partial index speeds up queries on the whole table equally."
[OK] Correct: The index only helps queries that match the WHERE condition; other queries still scan the full table.
Understanding partial indexes shows you can make queries faster by focusing on important data, a useful skill in real projects.
"What if the WHERE clause in the partial index covered more rows? How would that affect the time complexity?"
Practice
WHERE clause in PostgreSQL?Solution
Step 1: Understand partial index concept
A partial index indexes only a subset of rows based on a condition.Step 2: Role of WHERE clause
The WHERE clause defines which rows to include in the index.Final Answer:
To index only rows that meet a specific condition -> Option AQuick Check:
Partial index = conditional indexing [OK]
- Thinking partial indexes index all rows
- Confusing partial index with table backup
- Assuming partial index deletes rows
orders for rows where status = 'pending'?Solution
Step 1: Check index creation syntax
CREATE INDEX requires index name, table, columns, and optional WHERE clause.Step 2: Validate WHERE clause and operators
Use single equals (=) for comparison, and specify columns to index.Final Answer:
CREATE INDEX idx_pending ON orders (status) WHERE status = 'pending'; -> Option AQuick Check:
Correct syntax includes columns and WHERE with = [OK]
- Omitting columns in index definition
- Using double equals (==) instead of single =
- Placing WHERE before columns
CREATE INDEX idx_active_users ON users (last_login) WHERE active = true;What will be the result of this query?
SELECT * FROM users WHERE active = true AND last_login > '2024-01-01';Solution
Step 1: Understand partial index condition
The index covers rows where active = true, indexing last_login.Step 2: Analyze query filter
The query filters on active = true and last_login > '2024-01-01', matching the index condition.Final Answer:
The query will use the partial index and return matching rows quickly -> Option DQuick Check:
Query matches partial index condition = uses index [OK]
- Assuming partial index causes syntax errors
- Thinking partial index excludes all rows
- Believing query ignores partial index if condition partially matches
CREATE INDEX idx_recent_orders ON orders (order_date) WHERE order_date > CURRENT_DATE - INTERVAL '30 days';But queries filtering
order_date > CURRENT_DATE - INTERVAL '30 days' are not using the index. What is the likely problem?Solution
Step 1: Check partial index WHERE clause
The WHERE clause uses CURRENT_DATE, which is non-immutable (changes daily).Step 2: Understand index usage limitation
PostgreSQL partial indexes require immutable conditions to be used by queries.Final Answer:
The partial index condition uses a non-immutable function, so it cannot be used -> Option CQuick Check:
Non-immutable functions block partial index usage [OK]
- Assuming syntax error causes issue
- Thinking partial indexes can't use date columns
- Ignoring function immutability rules
products table filtering only active products with price < 100. Which partial index is best to create?Solution
Step 1: Define index condition matching query filters
The query filters active = true AND price < 100, so index WHERE must match both.Step 2: Check syntax and columns
Index must specify columns (price) and use AND in WHERE clause for correct filtering.Final Answer:
CREATE INDEX idx_active_price ON products (price) WHERE active = true AND price < 100; -> Option BQuick Check:
Partial index WHERE matches query filters exactly [OK]
- Using OR instead of AND in WHERE clause
- Omitting columns in index definition
- Trying to create index without columns
