What is an Index?
An index is like a roadmap for your data. It helps the database engine quickly find records without scanning the entire table. Just like the index of a book — instead of flipping every page, you check the index and go directly to the right page. 🔷 Clustered Index: Data is Physically Ordered A Clustered Index defines the physical order of rows in a table based on the indexed column. The data itself is sorted and stored on disk in the same order as the index. ✅ Key Points: Only one clustered index is allowed per table. The data rows themselves are part of the index. Very efficient for range queries and sorting operations. 📌 Real-life Example: If students are stored in order of their StudentID, and you're looking for ID 300, the database can jump straight to it without scanning everything. 🟡 Non-Clustered Index: Separate Structure for Searching A Non-Clustered Index is stored separately from the actual table data. It contains indexed values and a pointer to the location of the actual data row. ✅ Key Points: You can have multiple non-clustered indexes on a table. It does not change the physical order of data. Best for columns that are frequently filtered or searched. 📌 Lookup Process: The engine searches the index for the value. It uses the pointer to fetch the full row from the main table (this is called a Key Lookup). 📌 Real-life Example: If you're searching for Name = 'Sarah', the index quickly locates "Sarah" and points to her full record in the base table. 📊 Summary of Differences (in plain words) A Clustered Index stores the data physically sorted by the indexed column. A Non-Clustered Index uses a separate structure and just points to the data. Only one clustered index is allowed per table, but you can have many non-clustered indexes. Clustered indexes are best for range queries or sorting. Non-clustered indexes are best for frequent lookups on columns like names, emails, or statuses. 🎯 When to Use Each? Use a Clustered Index on: Primary keys Date fields Columns commonly sorted or queried in ranges Use a Non-Clustered Index on: Columns that are filtered often (e.g., name, email, status) Fields used in reporting or search operations Example for Clarity