What if you could find any piece of data instantly without flipping through everything?
Sequential scan vs index scan in PostgreSQL - When to Use Which
Imagine you have a huge phone book and you want to find all people named "John." You start at the first page and look at every single name until you find all the Johns.
This method is very slow and tiring because you check every name, even if the Johns are only on a few pages. It wastes time and energy, especially when the phone book is very big.
Using an index is like having a special list that tells you exactly which pages have the name "John." You can jump straight to those pages without flipping through the whole book.
SELECT * FROM people WHERE name = 'John'; -- scans all rowsCREATE INDEX idx_name ON people(name);
SELECT * FROM people WHERE name = 'John'; -- uses index to find rows fasterThis lets the database find data quickly and efficiently, saving time and resources.
When you search for a product on an online store, the system uses indexes to quickly show matching items instead of checking every product one by one.
Sequential scan checks every row one by one, which is slow for big data.
Index scan uses a special shortcut to find data faster.
Indexes make searching large databases much quicker and efficient.