Sobes.tech
Middle

If an index is created for two columns, will it work if the query contains only one of these columns?

sobes.tech AI

Answer from AI

As a rule, a composite index created for multiple columns (for example, (column_1, column_2)) can be used in a query that contains only the first column of the index (WHERE column_1 = ...).

However, if the query contains only the second column (WHERE column_2 = ...), the composite index usually will not be used for efficient searching, because the index is primarily ordered by the first column.

Example:

An index is created on (last_name, first_name):

  • The query WHERE last_name = 'Smith' can use this index.
  • The query WHERE first_name = 'John', is unlikely to efficiently use this index.

It is important to note that the specific behavior may depend on the database management system (DBMS) used and its query optimizer. Some DBMSs may use the index for partial scanning in certain cases.