Sobes.tech
Junior — Middle

Can you explain what a composite index is and what its features are in databases?

sobes.tech AI

Answer from AI

A composite index is an index in a database that is created based on multiple columns (fields) of a table. It helps to speed up search and sorting by the combination of these fields.

Features of a composite index:

  • The index stores values of multiple columns together, which is useful for queries filtering by several fields simultaneously.
  • The order of columns in the index is important: the index is effectively used if queries filter by the first columns of the index.
  • A composite index can replace several individual indexes, saving space and improving performance for complex queries.

Example of creating a composite index in SQL:

CREATE INDEX idx_name ON table_name (column1, column2);

This index will be useful for queries with conditions on column1 and column2, for example:

SELECT * FROM table_name WHERE column1 = 'value' AND column2 = 'value2';