0
0
Ruby on Railsframework~3 mins

Why Index creation in Ruby on Rails? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your database could find anything instantly, no matter how big it grows?

The Scenario

Imagine you have a huge phone book with thousands of names and numbers. If you want to find one person's number, you have to flip through every page until you find it.

The Problem

Searching manually through all entries takes a lot of time and effort. It's easy to lose your place or make mistakes, especially as the list grows bigger.

The Solution

Creating an index is like adding an organized list at the front of the phone book. It lets you jump straight to the right page quickly without flipping through everything.

Before vs After
Before
SELECT * FROM users WHERE email = 'example@example.com'; -- scans whole table
After
CREATE INDEX index_users_on_email ON users(email);
SELECT * FROM users WHERE email = 'example@example.com'; -- uses index for fast search
What It Enables

Indexes let databases find data lightning fast, even in huge tables, making apps feel quick and smooth.

Real Life Example

When you log into a website, the system quickly finds your user info by using an index on your email, so you don't have to wait.

Key Takeaways

Manual searching is slow and error-prone.

Indexes organize data for quick access.

They make large databases fast and efficient.