Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
Normalization in SQL Server
DDL and DML in SQL Server
SQL Server System Databases
SQL Server System Tables
Joins in SQL Server
SQL Server - Indexes
CTE in SQL Server
SQL Server - Locks
Optimization Techniques in SQL Server
How to Setup NDF Files in SQL Server
Deadlocks in SQL Server
Transactions in SQL Server
Difference UDF vs Stored Procedure
SQL Server Triggers
SQL Server Isolation Levels
SQL Server Pivot Tables
SQL Server - Group By Statement
SQL Server - Find Second highest Number Query
SQL Server - Recursion in User Defined Function
SQL Server Error Handling
Difference Table Variable and Temporary Table
SQL Server - Temporal Tables
User Defined Functions - SQL Server
Stored Procedures - SQL Server

SQL Server - Indexes



Indexes in SQL Server are used to speed up the retrieval of data from a table by providing a structured way to access rows quickly. Here are the main types of indexes:

  1. Clustered Index:

    • Only one per table.

    • Sorts and stores the data rows in the table based on the key columns.

    • The table itself is organized according to the index.

    • Example:

    sql
    CREATE CLUSTERED INDEX idx_cl_mycolumn ON mytable(mycolumn);
    
  2. Non-Clustered Index:

    • Multiple non-clustered indexes can be created per table.

    • Contains a sorted copy of the indexed columns and a pointer back to the actual data rows.

    • Example:

    sql
    CREATE NONCLUSTERED INDEX idx_ncl_mycolumn ON mytable(mycolumn);
    
  3. Unique Index:

    • Ensures that no duplicate values exist in the indexed column(s).

    • Can be either clustered or non-clustered.

    • Example:

    sql
    CREATE UNIQUE INDEX idx_unique_mycolumn ON mytable(mycolumn);
    
  4. Full-Text Index:

    • Used for full-text searches on large text columns.

    • Allows sophisticated word-based searches on textual data.

    • Example:

    sql
    CREATE FULLTEXT INDEX ON mytable(mycolumn) 
    KEY INDEX myprimarykey;
    
  5. Columnstore Index:

    • Used for data warehousing and analytics.

    • Stores data in a columnar format which enhances query performance for large datasets.

    • Example:

    sql
    CREATE COLUMNSTORE INDEX idx_columnstore ON mytable(mycolumn);
    
  6. Filtered Index:

    • Non-clustered index that includes a subset of rows in the table.

    • Useful for indexing a portion of the data.

    • Example:

    sql
    CREATE NONCLUSTERED INDEX idx_filtered ON mytable(mycolumn)
    WHERE mycolumn IS NOT NULL;
    

Indexes play a crucial role in optimizing the performance of database queries. Proper indexing can significantly improve query execution times, making your database operations much more efficient.

Would you like to delve deeper into any specific type of index or how to manage them?




All rights reserved | Privacy Policy | Sitemap