Re: Indexes and sorting

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: John Siracusa <siracusa(at)mindspring(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Indexes and sorting
Date: 2004-02-06 18:58:12
Message-ID: 20040206105232.X90689@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 6 Feb 2004, John Siracusa wrote:

> Are indexes useful for speeding up ORDER BY clauses? Example:
>
> CREATE TABLE t
> (
> a INT,
> b INT,
> c INT,
> d INT
> );
>
> SELECT * FROM t WHERE a = 1 AND b = 2 AND c = 3 ORDER BY b;
>
> Let's say the table just has one index:
>
> CREATE INDEX b_idx ON t (b);
>
> In this case, obviously the b_idx will be used and no sorting after the fact
> will be required. Now let's add an index:
>
> CREATE INDEX key_idx ON t (a, b, c);

If you're really doing the above alot, you probably really want (b,a,c)
which can probably avoid the sort as well (unless of course you're also
doing frequent sorts on a, etc...)

> On the same query, now the key_idx will be used and there'll be a sort
> wrapped around it all. The question is, is the b_idx useful at all anymore?

Yes. Queries searching on just b won't use key_idx.

> Can it be used to speed up the sort step? If so, how? If not, why not?

Not really at least right now.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joe Conway 2004-02-06 19:04:00 Re: how can I select into an array?
Previous Message Jenny Zhang 2004-02-06 18:34:20 Re: pgsql 7.4.1 copy - end-of-copy marker corrupt