π An Index in SQL is a special database object that improves the speed of data retrieval. Instead of scanning every row in a table, the database uses an index to quickly locate the required records, making queries significantly faster, especially for large tables.
π What is an Index?
An index is similar to the index found at the end of a book. Rather than reading every page to find a topic, you use the index to jump directly to the correct page. Likewise, a SQL index helps the database find rows without scanning the entire table.
Information
π― Why Use Indexes?
Indexes are essential for improving the performance of database queries, especially when working with large datasets.
- π Speed up data retrieval.
- π Reduce full table scans.
- π Improve filtering with WHERE.
- π Optimize sorting using ORDER BY.
- π Improve table joins.
- π Enhance overall application performance.
π How an Index Works
Without an index, the database often performs a full table scan, checking every row until it finds matching data. With an index, the database can quickly navigate to the required rows, reducing the number of rows it needs to examine.
| Without Index | With Index |
|---|---|
| Scans the entire table. | Looks up rows using the index. |
| Slower for large tables. | Much faster for searches. |
| Higher disk reads. | Fewer disk reads. |
π Sample Table
Consider the following Students table:
| StudentID | Name | Department | |
|---|---|---|---|
| 101 | Alice | Computer Science | alice@example.com |
| 102 | Bob | Mathematics | bob@example.com |
| 103 | Charlie | Physics | charlie@example.com |
π Creating an Index
Use the CREATE INDEX statement to create an index on one or more columns.
Create an Index
CREATE INDEX idx_student_name
ON Students(Name);This index helps the database quickly search for students by their Name.
π Searching with an Index
After creating the index, queries filtering by the indexed column can execute more efficiently.
Search Using an Indexed Column
SELECT *
FROM Students
WHERE Name = 'Alice';Tip
π Types of SQL Indexes
| Index Type | Description |
|---|---|
| Single-Column Index | Created on one column. |
| Composite Index | Created on multiple columns. |
| Unique Index | Prevents duplicate values while improving search performance. |
| Primary Key Index | Automatically created by many database systems for primary keys. |
| Clustered Index | Stores table data in index order (supported by some databases). |
| Non-Clustered Index | Stores a separate structure that points to table rows. |
πΌ Real-World Example
An online shopping website stores millions of products. Customers frequently search by product name and category. Creating indexes on these columns allows search results to appear much faster.
Index on Product Name
CREATE INDEX idx_product_name
ON Products(ProductName);βοΈ Indexed vs Non-Indexed Columns
| Feature | Indexed Column | Non-Indexed Column |
|---|---|---|
| Search Speed | Fast | Slower |
| Storage Requirement | Higher | Lower |
| INSERT Performance | Slightly slower | Faster |
| UPDATE Performance | Slightly slower | Faster |
| DELETE Performance | Slightly slower | Faster |
β οΈ When Should You Create an Index?
- β Columns frequently used in WHERE clauses.
- β Columns used in JOIN conditions.
- β Columns frequently sorted using ORDER BY.
- β Columns used in GROUP BY operations.
- β Frequently searched primary or foreign key columns.
β οΈ When Should You Avoid Too Many Indexes?
- β Small tables where full scans are inexpensive.
- β Columns that change frequently.
- β Columns with very few distinct values in some workloads.
- β Creating indexes on every column without analyzing query patterns.
Warning
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π Indexes improve query performance by speeding up data retrieval.
- π They reduce the need for full table scans.
- π Indexes consume additional storage space.
- π Write operations may become slightly slower because indexes must be maintained.
- π The database optimizer automatically decides when to use an index.
- π Well-designed indexes are essential for scalable, high-performance databases.