IntermediatePerformance & Indexes
Hash index for equality-only queries
The query
SQL
-- Hash index: faster for = comparisons, smaller than B-tree
CREATE INDEX idx_emp_status_hash ON employees USING HASH(status);
-- Works only for equality:
SELECT * FROM employees WHERE status = 'active'; -- uses hash index
-- Does NOT work for range:
SELECT * FROM employees WHERE status > 'a'; -- cannot use hash index
-- Check index type
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'employees';Note
“Hash indexes are faster for equality lookups but don't support range queries. Use B-tree for range/sort.