From: | Edmund Dengler <edmundd(at)eSentire(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Spped of max |
Date: | 2002-05-14 17:27:30 |
Message-ID: | Pine.BSO.4.40.0205141317400.6987-100000@olympus.esentire.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Greetings all!
Quick question: how long should finding the maximum value in a column
take?
Table: syslog_event
Column | Type | Modifiers
------------------+--------------------------+-----------
event_id | bigint | not null
...
Index: syslog_event_pkey
Column | Type
----------+--------
event_id | bigint
unique btree (primary key)
The query:
explain select event_id, timestamp, clean_message from syslog_event order
by event_id desc limit 5;
NOTICE: QUERY PLAN:
Limit (cost=0.00..3.49 rows=5 width=119)
-> Index Scan Backward using syslog_event_pkey on syslog_event (cost=0.00..2435803.36 rows=3490163 width=119)
and this returns almost instantaneously as expected.
explain select max(event_id) from syslog_event;
NOTICE: QUERY PLAN:
Aggregate (cost=237923.04..237923.04 rows=1 width=8)
-> Seq Scan on syslog_event (cost=0.00..229197.63 rows=3490163 width=8)
This takes forever!
Now, shouldn't this query be answered out of index (possibly with a check
for validity if necessary)?
I mean, isn't this really equivalent to:
explain select max(event_id) from (select event_id from syslog_event order
by event_id desc limit 1) as syslog_event;
NOTICE: QUERY PLAN:
Aggregate (cost=0.70..0.70 rows=1 width=8)
-> Subquery Scan syslog_event (cost=0.00..0.70 rows=1 width=8)
-> Limit (cost=0.00..0.70 rows=1 width=8)
-> Index Scan Backward using syslog_event_pkey on syslog_event (cost=0.00..2435803.36 rows=3490163 width=8)
which flies as expected? Now, this type of thing gets me real worried
about how good the optimizer really is. I have a number of fairly
complicated queries that are created via criteria from the web, and it
would be a pain to get them all "hand-optimized" if I cannot rely on the
optimizer at least picking reasonable methods.
Miscellaneous: Using 7.2.1 under OpenBSD 3.0
Ed
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2002-05-14 17:35:45 | Re: transitioning postgres oid |
Previous Message | Corey W. Gibbs | 2002-05-14 17:26:28 | Re: Using COPY |