Junior — Middle
How is an index on two columns implemented in PostgreSQL?
sobes.tech AI
Answer from AI
In PostgreSQL, an index on two columns is implemented as a composite (or concatenated) index. Such an index is created on multiple table columns and allows efficient execution of queries that filter or sort data based on these columns.
For example, if you create an index:
CREATE INDEX idx_example ON table_name (column1, column2);
then the index will store the values of column1 and, for each value of column1, the sorted values of column2. This enables quick searches for records based on column1 and column2, as well as efficient use of the index for queries where filtering or sorting starts with column1.
It is important that the order of columns in the index matters: an index on (column1, column2) will not be efficiently used for queries filtering only by column2.