Hash index for equality in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a hash index in a database, it helps us find rows quickly by matching exact values.
We want to know how the time to find data changes as the table grows bigger.
Analyze the time complexity of the following query using a hash index.
CREATE INDEX idx_hash_name ON users USING hash (name);
SELECT * FROM users WHERE name = 'Alice';
This code creates a hash index on the column name and then searches for rows where name equals 'Alice'.
Look at what happens when the query runs:
- Primary operation: Computing the hash of the search value and looking up the matching bucket.
- How many times: Once per query, no scanning through all rows.
As the table grows, the hash index keeps the search fast by jumping directly to the right place.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 to 2 steps |
| 100 | Still about 1 to 2 steps |
| 1000 | Still about 1 to 2 steps |
Pattern observation: The number of steps stays almost the same no matter how big the table gets.
Time Complexity: O(1)
This means the time to find a row by exact match using a hash index stays constant even if the table grows very large.
[X] Wrong: "Searching with a hash index takes longer as the table gets bigger because it has to check more rows."
[OK] Correct: The hash index jumps directly to the matching entries, so it does not scan all rows. The search time stays about the same.
Understanding how hash indexes keep searches fast helps you explain efficient data lookup in real projects and shows you know how databases handle big data smoothly.
"What if we changed the query to search with a range condition instead of exact match? How would the time complexity change?"
Practice
What is the main advantage of using a hash index in PostgreSQL?
Solution
Step 1: Understand hash index purpose
Hash indexes are designed to speed up searches where you look for exact matches (equality) on a column.Step 2: Compare with other index types
Unlike B-tree indexes, hash indexes do not support range queries or ordering, so they are not useful for those.Final Answer:
It speeds up equality searches on a column. -> Option AQuick Check:
Hash index = equality speedup [OK]
- Thinking hash indexes speed up range queries
- Confusing hash indexes with data compression
- Assuming hash indexes handle foreign keys automatically
Which of the following is the correct syntax to create a hash index on the email column of a table named users?
?
Solution
Step 1: Recall hash index syntax
The correct syntax uses CREATE INDEX, specifies the index name, the table, and uses USING hash to indicate a hash index.Step 2: Check each option
CREATE INDEX users_email_hash ON users USING hash (email); matches the correct syntax exactly. The other options have syntax errors or use the wrong index type.Final Answer:
CREATE INDEX users_email_hash ON users USING hash (email); -> Option DQuick Check:
CREATE INDEX ... USING hash ... [OK]
- Using CREATE HASH INDEX instead of CREATE INDEX
- Forgetting 'USING hash' clause
- Using btree instead of hash for hash index
Given the table products(id INT, name TEXT) with a hash index on id, what will the query SELECT * FROM products WHERE id = 10; most likely use?
Solution
Step 1: Identify query condition type
The query uses an equality condition on theidcolumn:id = 10.Step 2: Match index type to query
Since there is a hash index onid, PostgreSQL will use a hash index scan to quickly find rows whereidequals 10.Final Answer:
A hash index scan for fast equality lookup -> Option BQuick Check:
Equality query + hash index = hash index scan [OK]
- Assuming sequential scan always happens
- Confusing bitmap index with hash index
- Thinking hash index supports range queries
Consider the following SQL commands:CREATE TABLE employees(id INT, name TEXT);
CREATE INDEX emp_id_hash ON employees USING hash (id);
SELECT * FROM employees WHERE id > 5;
What is the problem with using the hash index in this query?
Solution
Step 1: Understand hash index limitations
Hash indexes only support equality searches, not range conditions likeid > 5.Step 2: Analyze the query condition
The query uses a range condition, so the hash index cannot be used efficiently here.Final Answer:
Hash indexes do not support range queries likeid > 5. -> Option CQuick Check:
Range query + hash index = no use [OK]
- Thinking hash indexes support range queries
- Believing index names must follow special rules
- Assuming primary key is required for hash index
You have a large table orders(order_id INT, customer_id INT, status TEXT). You often query orders by customer_id with equality conditions, but sometimes you query by status with range-like conditions (e.g., status > 'A'). Which indexing strategy is best?
Solution
Step 1: Match index types to query patterns
Hash indexes are good for equality searches, so use one oncustomer_id. B-tree indexes support range queries, so use one onstatus.Step 2: Evaluate options
Create a hash index oncustomer_idand a B-tree index onstatus. correctly assigns hash index for equality and B-tree for range. Create hash indexes on bothcustomer_idandstatus. wrongly uses hash for range. Create a B-tree index oncustomer_idonly. misses index onstatus. Create no indexes to avoid overhead. ignores performance.Final Answer:
Create a hash index oncustomer_idand a B-tree index onstatus. -> Option AQuick Check:
Equality = hash, range = B-tree [OK]
- Using hash index for range queries
- Not indexing frequently queried columns
- Avoiding indexes due to overhead without reason
