Re: Why sequential scan when there's a supporting index?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ron Johnson <ron(dot)l(dot)johnson(at)cox(dot)net>
Cc: PgSQL Novice ML <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Why sequential scan when there's a supporting index?
Date: 2002-05-24 13:54:06
Message-ID: 3024.1022248446@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Ron Johnson <ron(dot)l(dot)johnson(at)cox(dot)net> writes:
> test2=# explain select max(tx_date) from t_lane_tx;
> NOTICE: QUERY PLAN:

> Aggregate (cost=2209999.20..2209999.20 rows=1 width=4)
> -> Seq Scan on t_lane_tx (cost=0.00..2125170.96 rows=33931296
> width=4)

I'm beginning to think this should be in the FAQ ...

Try it like this:

regression=# explain select unique1 from tenk1 order by unique1 desc limit 1;

Limit (cost=0.00..0.11 rows=1 width=4)
-> Index Scan Backward using tenk1_unique1 on tenk1 (cost=0.00..1071.99 rows=10000 width=4)

Although the LIMIT clause isn't standard, this approach is attractive
compared to max() because you can fetch any or all values in the row
containing the maximal element, which is a very useful thing. Also,
the approach scales to situations where you want to sort by multiple
columns.

Improving the handling of max() has been on the TODO list for awhile,
but most of the hacker community considers it low priority because of
the availability of the above workaround. Also, Postgres has a very
generalized black-box approach to aggregate functions, so no one's been
able to think of a reasonably clean way to teach the planner that some
aggregates are connected to index sort ordering.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Ron Johnson 2002-05-24 14:25:58 Re: Why sequential scan when there's a supporting index?
Previous Message Ron Johnson 2002-05-24 13:48:52 Re: Why sequential scan when there's a supporting index?