Sobes.tech
Junior — Middle

Is it possible to create a composite index on multiple columns in a database?

sobes.tech AI

Answer from AI

Yes, creating a composite (composite) index on multiple columns in a database is possible and widely used to optimize queries that filter or sort data by multiple fields.

For example, in SQL, you can create such an index as follows:

CREATE INDEX idx_user_lastname_firstname ON users (lastname, firstname);

This index will be useful for queries where both columns lastname and firstname are used in the condition, or only lastname (since the index is ordered first by the first column). However, if a query filters only by firstname, this index may not be used.

Composite indexes help speed up the execution of complex queries, but they should be created consciously to avoid degrading performance during inserts and updates.