Concept Flow - Why indexes speed up queries
Start Query
Check for Index
Use Index
Find Rows
Return Result
End
The database checks if an index exists for the query. If yes, it uses the index to find rows faster; if no, it scans the whole table.
SELECT * FROM users WHERE age = 30; -- Assume 'age' column has an index
| Step | Action | Index Used? | Rows Checked | Result |
|---|---|---|---|---|
| 1 | Start query execution | N/A | 0 | No rows yet |
| 2 | Check if 'age' column has index | Yes | 0 | Index found |
| 3 | Use index to find matching rows | Yes | Few (only matching age=30) | Rows found quickly |
| 4 | Return matching rows | Yes | Few | Result returned |
| 5 | End query | N/A | Few | Query complete |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| Index Used | No | Yes | Yes | Yes | Yes |
| Rows Checked | 0 | 0 | Few (matching rows) | Few | Few |
| Rows Returned | 0 | 0 | Few | Few | Few |
Why indexes speed up queries: - Indexes are like a book's index, pointing directly to data. - Database checks for index before searching. - Using index means fewer rows checked. - Without index, full table scan is done. - Indexes make queries faster by reducing search work.