Spped of max

Поиск
Список
Период
Сортировка
От Edmund Dengler
Тема Spped of max
Дата
Msg-id Pine.BSO.4.40.0205141317400.6987-100000@olympus.esentire.com
обсуждение исходный текст
Ответы Re: Spped of max
Список 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




В списке pgsql-general по дате отправления:

Предыдущее
От: Philip Hallstrom
Дата:
Сообщение: Re: Question about SERIAL
Следующее
От: Tom Lane
Дата:
Сообщение: Re: transitioning postgres oid