SQL FOUNDATIONS:Indexes & Performance Basics
Learn how indexes speed up queries and when to use them.
SQL Indexes & Performance Basics Explained with Examples + Practice Questions
When to Use Indexes
Use indexes to speed up data retrieval.
Especially useful for:
- Large tables
- Frequent searches
- JOIN operations
What is an Index
An index is a data structure that helps the database find rows faster, similar to an index in a book.
Without index:
- Database scans entire table (slow)
With index:
- Database jumps directly to required data (fast)
Creating an Index
sqlCREATE INDEX idx_departmentON employees(department);
How It Works
Query:
sqlSELECT * FROM employeesWHERE department = 'IT';
Without index → full table scan
With index → direct lookup
Types of Indexes
- Single Column Index
sqlCREATE INDEX idx_salaryON employees(salary);
- Composite Index
sqlCREATE INDEX idx_dept_salaryON employees(department, salary);
Used when filtering on multiple columns.
- Unique Index
sqlCREATE UNIQUE INDEX idx_emailON employees(email);
Ensures no duplicate values.
Dropping an Index
sqlDROP INDEX idx_department;
When Index is Useful
- WHERE conditions
- JOIN columns
- ORDER BY columns
- Large datasets
When Index is NOT Useful
- Small tables
- Columns with many duplicate values
- Frequently updated columns
Performance Concepts
- Full Table Scan vs Index Scan
- Full scan checks every row
- Index scan uses shortcut
Write vs Read Trade-off
- Index improves read speed
- But slows INSERT / UPDATE / DELETE
Key Concepts
- Index improves query performance
- Works best on frequently searched columns
- Too many indexes can slow down writes
- Balance is important
SQL Performance Tips
- Avoid SELECT *
- Use proper indexes
- Filter early with WHERE
- Use LIMIT when possible
- Avoid unnecessary subqueries
Common Questions (FAQ)
What is an index in SQL
A structure that improves query speed.
Does index improve performance
Yes for reads, but may slow writes.
When should we create index
On frequently searched columns.
Can too many indexes be harmful
Yes, they increase storage and slow updates.
Internal Learning Links
- Learn WHERE filtering
- Learn JOINs
- Learn ORDER BY
Practice CTA
Practice performance-based SQL questions on Schoolabe to write efficient queries.
Indexes & Performance Basics Missions
Solve exercises in sequence to unlock the next mission.
Index on salary
Create an index on salary column.
Composite index
Create a composite index on department and salary.
Unique index on email
Create a unique index on email column.
Drop index on department
Drop index on department.
Query with salary filter
Identify which query benefits from index: WHERE salary > 50000.
Small table note
Identify if index helps for small table.
Use indexed column
Write query that uses indexed column department.
Why too many indexes are bad
Explain why too many indexes are bad.
Best index for query
Choose correct index for query: department + salary filter.