Re: "select max/count(id)" not using index - Mailing list pgsql-performance

From Christopher Kings-Lynne
Subject Re: "select max/count(id)" not using index
Date
Msg-id 3FE6CDF2.10608@familyhealth.com.au
Whole thread Raw
In response to "select max/count(id)" not using index  (Ryszard Lach <siaco@autograf.pl>)
List pgsql-performance
> I have a table with 24k records and btree index on column 'id'. Is this
> normal, that 'select max(id)' or 'select count(id)' causes a sequential
> scan? It takes over 24 seconds (on a pretty fast machine):
>
> => explain ANALYZE select max(id) from ogloszenia;

Yes, it is.  It is a known issue with Postgres's extensible operator
architecture.

The work around is to have an index on the id column and do this instead:

SELECT id FROM ogloszenia ORDER BY id DESC LIMIT 1;

Which will be really fast.

Chris


pgsql-performance by date:

Previous
From: Ryszard Lach
Date:
Subject: "select max/count(id)" not using index
Next
From: Pavel Stehule
Date:
Subject: Re: "select max/count(id)" not using index