Indexes on Aggregate Functions - Mailing list pgsql-general

From Curt Sampson
Subject Indexes on Aggregate Functions
Date
Msg-id Pine.NEB.4.43.0207011513000.408-100000@angelic.cynic.net
Whole thread Raw
In response to Re: serial columns & loads misfeature?  (Jason Earl <jason.earl@simplot.com>)
Responses Re: Indexes on Aggregate Functions  (Alvaro Herrera <alvherre@atentus.com>)
List pgsql-general
On 28 Jun 2002, Jason Earl wrote:

> SELECT setval('test_s_seq', (SELECT max(s) + 1 FROM test));
>
> PostgreSQL doesn't use the indexes on aggregate functions (like max())
> so it would be faster on large tables to write that as:
>
> SELECT setval('test_s_seq', (SELECT s + 1 FROM test ORDER BY s DESC
> LIMIT 1));

I've wondered about this, actually. Why doesn't postgres use the
indexes? For something like MAX(s) it would certainly be a lot faster.

Another cool optimisation that MS SQL Server does is, if the
information requested is looked up in an index, and all the columns
you're retrieving are already in the index data, it doesn't bother
retrieving the values from the table itself (though presumably it
checks for triggers to execute and so on). E.g., if you have

    CREATE TABLE foo (
    mykey        int PRIMARY KEY,
    otherkey_part1     int NOT NULL,
    otherkey_part2    varchar(255) NOT NULL,
    morestuff    varchar(255) NOT NULL);
    CREATE INDEX otherkeys ON foo (otherkey_part1, otherkey_part2);
    SELECT otherkey_part1, otherkey_part2 FROM foo
    WHERE otherkey_part1 = 17;

that query will read only the index, not the table itself.

cjs
--
Curt Sampson  <cjs@cynic.net>   +81 90 7737 2974   http://www.netbsd.org
    Don't you know, in this new Dark Age, we're all light.  --XTC




pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: literature about search-algorithms
Next
From: Alvaro Herrera
Date:
Subject: Re: Indexes on Aggregate Functions