DEV Community

Cover image for Indexes: Quickstart Using PostgreSQL (15 sec read)
EffessDev
EffessDev

Posted on

Indexes: Quickstart Using PostgreSQL (15 sec read)

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);
Enter fullscreen mode Exit fullscreen mode

Now when you run

SELECT * FROM users WHERE name = 'Emily';
Enter fullscreen mode Exit fullscreen mode

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)