... - Mailing list pgsql-sql

From Tom Lane
Subject ...
Date
Msg-id 19199.937406097@sss.pgh.pa.us
Whole thread Raw
In response to ...  (Pavel Mamin <pm@sky.ru>)
List pgsql-sql
Pavel Mamin <pm@sky.ru> writes:
> .. and I want to make the next query:
>> select field1, field2, field3
>> from tab1
>> where field2=<some value>
>> order by field3;

> What indexes I have to make?

You don't *have* to make any indexes.  But if you do a lot of queries
that use field2 in the WHERE condition, an index on field2 would make
them faster.

I don't think an index on field3 would be helpful for this query.
Unless a very large fraction of the rows in tab1 have the same
field2 value you are selecting for, it's going to be fastest to
pull out the matching rows using a field2 index and then sort them
on field3.  Scanning the table with a field3 index would deliver the
result already sorted --- but you'd have to visit every row in order
to see whether it matched the WHERE clause, so that'd be a loser.

In theory an index on (field2, field3) would be the perfect thing for
this particular query, but Postgres isn't actually smart enough to
realize that it could use such an index and not have to sort on
field3.  Anyway, such an index would be so specialized that it probably
wouldn't be worth the maintenance costs...
        regards, tom lane


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: [SQL] SELECT DISTINCT and ORDER BY
Next
From: "Albert REINER"
Date:
Subject: Re: [SQL] Permission problem with COPY FROM