I am creating indexes for a PostgreSQL database. I would like to know whether the column(s) used to order results in a PostgreSQL statement should be included in the index.
Assume I have created a table with label 'table1' in a PostgreSQL database with columns labelled 'col1', 'col2' and 'col3'.
I would like to execute the following query:
SELECT * FROM table1 WHERE col1 = 'word1' AND col2 = 'word2' ORDER BY col3;
I know that an index for this search should include all columns referenced in the WHERE
clause so, in this case, the index would include col1 and col2.
Should the index also include col3?
col1, col2
, then order the results bycol3
. This completely changes if you are using inequalities>
<>
<=
etc – Charliefacecol1, col2, col3
don't forgetinclude (other_columns)
if your version supports it. And don't useselect *
if you can help it, specify the columns you need. – Charlieface