Let's consider a table user. When we execute a query to find Emily, we are actually going through each record, looking whether the name column equals Emily. Indexes comes in when you want to speed this up.
Let's create an Index with the name idx_users_name (the name can be anything, and it doesn't matter functionally):
CREATE INDEX idx_users_name ON users (name);
Now when you run
SELECT * FROM users WHERE name = 'Emily';
Postgres will use the index we just created (not by the name, the name is just for us) to execute that query, and the time complexity is reduced from O(n) to O(log n).
Top comments (0)