Sobes.tech
Middle

What types of indexes exist in SQL?

sobes.tech AI

Answer from AI

In SQL, there are various types of indexes designed to optimize query performance. Main types:

  • Clustered indexes: Physically sort data rows by the index key. A table can have only one clustered index.
  • Non-clustered indexes: Have a separate structure containing index keys and pointers to data rows in the table. A table can have multiple non-clustered indexes.
  • Unique indexes: Ensure the uniqueness of values in a column (or a combination of columns). They can be either clustered or non-clustered.
  • Covering (or full) indexes: Include all columns needed to satisfy a query, avoiding the need to access the table for additional data. They can be clustered or non-clustered.
  • Expression (or functional) indexes: Created based on the result of an expression or function applied to one or more columns.
  • Text indexes: Designed for searching text within large text columns. Often implemented using specialized algorithms.
  • Spatial indexes: Used for efficient searching in geographic or geometric data.

Example of creating a non-clustered index:

-- Creating a non-clustered index on the last_name column of the employees table
CREATE INDEX idx_lastname ON employees (last_name);