pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedselect max(id) from aTable is very slow
5+ messages / 3 participants
[nested] [flat]
* select max(id) from aTable is very slow
@ 2004-02-16 16:51 David Teran <david.teran@cluster9.com>
2004-02-16 17:56 ` Re: select max(id) from aTable is very slow Nick Barr <nicky@chuckie.co.uk>
2004-06-03 13:07 ` Re: select max(id) from aTable is very slow Richard Huxton <dev@archonet.com>
0 siblings, 2 replies; 5+ messages in thread
From: David Teran @ 2004-02-16 16:51 UTC (permalink / raw)
To: pgsql-performance
Hi,
we have a table with about 6.000.000 rows. There is an index on a
column with the name id which is an integer and serves as primary key.
When we execute select max(id) from theTable; it takes about 10
seconds. Explain analyze returns:
------------------------------------------------------------------------
--------------------------------------------------------
Aggregate (cost=153635.15..153635.15 rows=1 width=4) (actual
time=9738.263..9738.264 rows=1 loops=1)
-> Seq Scan on job_property (cost=0.00..137667.32 rows=6387132
width=4) (actual time=0.102..7303.649 rows=6387132 loops=1)
Total runtime: 9738.362 ms
(3 rows)
I recreated the index on column id and ran vacuum analyze job_property
but this did not help. I tried to force index usage with SET
ENABLE_SEQSCAN TO OFF; but the explain analyze still looks like the
query is done using a seqscan.
Is the speed more or less normal for a 'dual G5 with 2 GHZ and 4 GB of
Ram and a SATA hd' or do i miss something?
regards David
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: select max(id) from aTable is very slow
2004-02-16 16:51 select max(id) from aTable is very slow David Teran <david.teran@cluster9.com>
@ 2004-02-16 17:56 ` Nick Barr <nicky@chuckie.co.uk>
2004-02-16 18:02 ` Re: select max(id) from aTable is very slow Nick Barr <nicky@chuckie.co.uk>
1 sibling, 1 reply; 5+ messages in thread
From: Nick Barr @ 2004-02-16 17:56 UTC (permalink / raw)
To: David Teran <david.teran@cluster9.com>; +Cc: pgsql-performance
David Teran wrote:
> Hi,
>
> we have a table with about 6.000.000 rows. There is an index on a
> column with the name id which is an integer and serves as primary key.
>
> When we execute select max(id) from theTable; it takes about 10
> seconds. Explain analyze returns:
>
> ------------------------------------------------------------------------
> --------------------------------------------------------
> Aggregate (cost=153635.15..153635.15 rows=1 width=4) (actual
> time=9738.263..9738.264 rows=1 loops=1)
> -> Seq Scan on job_property (cost=0.00..137667.32 rows=6387132
> width=4) (actual time=0.102..7303.649 rows=6387132 loops=1)
> Total runtime: 9738.362 ms
> (3 rows)
>
>
>
> I recreated the index on column id and ran vacuum analyze
> job_property but this did not help. I tried to force index usage
> with SET ENABLE_SEQSCAN TO OFF; but the explain analyze still looks
> like the query is done using a seqscan.
>
> Is the speed more or less normal for a 'dual G5 with 2 GHZ and 4 GB
> of Ram and a SATA hd' or do i miss something?
>
> regards David
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
Try using:
SELECT id FROM theTable ORDER BY is DESC LIMIT 1;
Using COUNT, MAX, MIN and any aggregate function on the table of that
size will always result in a sequential scan. There is currently no way
around it although there are a few work arounds. See the following for
more information.
http://archives.postgresql.org/pgsql-performance/2004-01/msg00045.php
http://archives.postgresql.org/pgsql-performance/2004-01/msg00054.php
http://archives.postgresql.org/pgsql-performance/2004-01/msg00059.php
HTH
Nick
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: select max(id) from aTable is very slow
2004-02-16 16:51 select max(id) from aTable is very slow David Teran <david.teran@cluster9.com>
2004-02-16 17:56 ` Re: select max(id) from aTable is very slow Nick Barr <nicky@chuckie.co.uk>
@ 2004-02-16 18:02 ` Nick Barr <nicky@chuckie.co.uk>
2004-02-16 18:15 ` Re: select max(id) from aTable is very slow David Teran <david.teran@cluster9.com>
0 siblings, 1 reply; 5+ messages in thread
From: Nick Barr @ 2004-02-16 18:02 UTC (permalink / raw)
To: Nick Barr <nicky@chuckie.co.uk>; +Cc: David Teran <david.teran@cluster9.com>; pgsql-performance
Nick Barr wrote:
> David Teran wrote:
>
>> Hi,
>>
>> we have a table with about 6.000.000 rows. There is an index on a
>> column with the name id which is an integer and serves as primary key.
>>
>> When we execute select max(id) from theTable; it takes about 10
>> seconds. Explain analyze returns:
>>
>> ------------------------------------------------------------------------
>> --------------------------------------------------------
>> Aggregate (cost=153635.15..153635.15 rows=1 width=4) (actual
>> time=9738.263..9738.264 rows=1 loops=1)
>> -> Seq Scan on job_property (cost=0.00..137667.32 rows=6387132
>> width=4) (actual time=0.102..7303.649 rows=6387132 loops=1)
>> Total runtime: 9738.362 ms
>> (3 rows)
>>
>>
>>
>> I recreated the index on column id and ran vacuum analyze
>> job_property but this did not help. I tried to force index usage
>> with SET ENABLE_SEQSCAN TO OFF; but the explain analyze still looks
>> like the query is done using a seqscan.
>>
>> Is the speed more or less normal for a 'dual G5 with 2 GHZ and 4 GB
>> of Ram and a SATA hd' or do i miss something?
>>
>> regards David
>>
>>
>> ---------------------------(end of broadcast)---------------------------
>> TIP 3: if posting/reading through Usenet, please send an appropriate
>> subscribe-nomail command to majordomo@postgresql.org so that your
>> message can get through to the mailing list cleanly
>
>
> Try using:
>
> SELECT id FROM theTable ORDER BY is DESC LIMIT 1;
>
> Using COUNT, MAX, MIN and any aggregate function on the table of that
> size will always result in a sequential scan. There is currently no
> way around it although there are a few work arounds. See the following
> for more information.
>
> http://archives.postgresql.org/pgsql-performance/2004-01/msg00045.php
> http://archives.postgresql.org/pgsql-performance/2004-01/msg00054.php
> http://archives.postgresql.org/pgsql-performance/2004-01/msg00059.php
>
> HTH
>
> Nick
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
Oops that should be
SELECT id FROM theTable ORDER BY id DESC LIMIT 1;
Nick
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: select max(id) from aTable is very slow
2004-02-16 16:51 select max(id) from aTable is very slow David Teran <david.teran@cluster9.com>
2004-02-16 17:56 ` Re: select max(id) from aTable is very slow Nick Barr <nicky@chuckie.co.uk>
2004-02-16 18:02 ` Re: select max(id) from aTable is very slow Nick Barr <nicky@chuckie.co.uk>
@ 2004-02-16 18:15 ` David Teran <david.teran@cluster9.com>
0 siblings, 0 replies; 5+ messages in thread
From: David Teran @ 2004-02-16 18:15 UTC (permalink / raw)
To: Nick Barr <nicky@chuckie.co.uk>; +Cc: pgsql-performance
Hi Nick,
>> Try using:
>>
>> SELECT id FROM theTable ORDER BY is DESC LIMIT 1;
>>
>> Using COUNT, MAX, MIN and any aggregate function on the table of that
>> size will always result in a sequential scan. There is currently no
>> way around it although there are a few work arounds. See the
>> following for more information.
>>
>> http://archives.postgresql.org/pgsql-performance/2004-01/msg00045.php
>> http://archives.postgresql.org/pgsql-performance/2004-01/msg00054.php
>> http://archives.postgresql.org/pgsql-performance/2004-01/msg00059.php
>>
thanks, that works fine! I will read the mail archive before asking
such things again ;-)
cheers David
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: select max(id) from aTable is very slow
2004-02-16 16:51 select max(id) from aTable is very slow David Teran <david.teran@cluster9.com>
@ 2004-06-03 13:07 ` Richard Huxton <dev@archonet.com>
1 sibling, 0 replies; 5+ messages in thread
From: Richard Huxton @ 2004-06-03 13:07 UTC (permalink / raw)
To: David Teran <david.teran@cluster9.com>; +Cc: pgsql-performance
David Teran wrote:
> Hi,
>
> we have a table with about 6.000.000 rows. There is an index on a
> column with the name id which is an integer and serves as primary key.
>
> When we execute select max(id) from theTable; it takes about 10
> seconds. Explain analyze returns:
Due to the open-ended nature of PG's aggregate function system, it can't
see inside the max() function to realise it doesn't need all the values.
Fortune favours the flexible however - the simple workaround is to use
the equivalent:
SELECT id FROM theTable ORDER BY id DESC LIMIT 1;
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2004-06-03 13:07 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-02-16 16:51 select max(id) from aTable is very slow David Teran <david.teran@cluster9.com>
2004-02-16 17:56 ` Nick Barr <nicky@chuckie.co.uk>
2004-02-16 18:02 ` Nick Barr <nicky@chuckie.co.uk>
2004-02-16 18:15 ` David Teran <david.teran@cluster9.com>
2004-06-03 13:07 ` Richard Huxton <dev@archonet.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox