O
O
Oversec2017-02-21 16:30:53
PostgreSQL
Oversec, 2017-02-21 16:30:53

Why do we need to add index to the fields in the table?

During the training in the Rails Tutorial, I came across the fact that when creating the users table, we added an index to the email field (add_index :users, :email), in the book itself it was somehow casually explained why (for faster search by email, so that it was not necessary to sort through all the emails). So how does this help with searches? Why do we actually need to add an index to the table fields? And to what fields it should be done?
Maybe a stupid question, but I want to understand this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2017-02-21
@Oversec

Imagine that you need to find a certain chapter in a book, but you don't know what page it is on. You will have to leaf through the entire book until you find it. But if the book has a table of contents, you will quickly skim it and see the page number you need. This is what indexes are for.
What fields are needed? If you answer in general - then for those fields on which you will impose where conditions, or sorting, or grouping in your sql queries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question