Indexes and Constraints Flashcards
What are the 6 types of indexes?
1- Clustered Index
2- Non-Clustered Index
3- Unique Index
4- Composite Index
5- Covered Index
6- Full-Text Index
Definition: “Clustered Index”
This type of index determines the physical order of data in a table. Each table can have only one clustered index. When a clustered index is created, the data in the table is physically reordered to match the order of the index. It is commonly used on columns that are frequently used for sorting or range queries.
Definition: “Non-Clustered Index”
Unlike clustered indexes, non-clustered indexes do not determine the physical order of data in a table. Instead, they create a separate structure that contains the indexed column(s) along with a reference to the corresponding row in the table. Non-clustered indexes are useful for improving the performance of search queries.
Definition: “Unique Index”
A unique index ensures that the values in the indexed column(s) are unique, meaning no duplicate values are allowed. It is similar to a non-clustered index, but with an additional constraint of uniqueness.
Definition: “Composite Index”
A composite index is created on multiple columns of a table. It allows for efficient searching and sorting based on combinations of the indexed columns. Composite indexes can be useful when queries involve multiple conditions that span multiple columns.
Definition: “Covered Index”
A covered index includes all the columns required to satisfy a query, thereby eliminating the need to access the actual table. It improves query performance by reducing disk I/O.
Definition: “Full-Text Index”
Full-text indexing is used for efficient text-based searching. It enables searching for words or phrases within large amounts of text stored in a column.
How many clustered indexes we can have on a table?
It’s important to note that a table can have only one clustered index because the clustered index determines the physical order of the data.
How can we set a clustered index?
CREATE CLUSTERED INDEX IDX_DimEmployee_EmpCode ON DimEmployee (EmpCode);
How can we update/change a clustered index?
Changing a clustered index involves dropping the existing clustered index and creating a new one:
DROP INDEX IDX_DimEmployee_EmpCode ON DimEmployee;
CREATE CLUSTERED INDEX IDX_DimEmployee_EmpCode ON DimEmployee (EmpCode);
How can we delete a clustered index?
DROP INDEX IDX_DimEmployee_EmpCode ON DimEmployee;
How can we have a list of indexes?
EXEC sp_helpindex DimEmployee
An example which gives indexes on DimEmployee Table
What is the relationship between clustered index and the primary key?
A clustered index is commonly used as the primary key constraint, providing uniqueness and efficient retrieval of data by the primary key.
What are the pros of the clustered index? (8)
Physical Data Order
Faster Data Retrieval
Eliminates Index Lookup
Covering Queries
Supports Primary Key Constraint
Improved Sorting
Data Integrity
Optimized Joins
What are the cons of the clustered index? (7)
Disk Space
Data Modification Performance
Impact on Insert Performance
Fragmentation
Limited Number of Clustered Indexes
Non-Covering Queries
Inflexible Order