Indexes and Constraints Flashcards
(39 cards)
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
How many non-clustered indexes we can have on a table?
In Microsoft SQL Server, the limit on the number of non-clustered indexes per table is 999. This limit includes both unique and non-unique non-clustered indexes. It’s important to note that this limit is for non-clustered indexes specifically and does not include the clustered index, which is limited to one per table.
How can we set a non-clustered index?
CREATE INDEX IDX_DimEmployee_EmpName
ON DimEmployee (EmpName);
How can we change a non-clustered index?
DROP INDEX IDX_DimEmployee_EmpName ON DimEmployee;
CREATE INDEX IDX_DimEmployee_EmpName
ON DimEmployee (EmpName);
What are pros of non-clustered index? (10)
Improved Query Performance
Flexible Index Design
Covering Queries
Efficient Data Modification
Index Joining
Index Scans and Seeks
Query Optimization
Conserve Disk Space
Data Integrity
Multiple Indexes
What are cons of non-clustered index? (10)
Increased Disk Space
Impact on Data Modification
Index Maintenance Overhead
Query Plan Complexity
Fragmentation
Data Duplication
Non-Covering Queries
Index Selection Overhead
Index Overhead for Low-Selectivity Columns
Index Management Complexity
What does “Data Manipulation” mean?
Insert, Delete, Update
What does “Low Selectivity” mean?
Columns with low selectivity are those with a relatively small number of distinct values.
What does “Covering Query” mean in the context of indexes?
An index can include all the columns required for a query. In this case, the query does not need to refer to the actual table to run.
How can we set a unique index?
CREATE UNIQUE INDEX IDX_DimEmployee ON DimDate (Year, Month, Day)