pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hiroshi Inoue <Inoue@tpf.co.jp>
To: Bruce Momjian <maillist@candle.pha.pa.us>
To: jwieck@debis.com
Cc: hackers@postgreSQL.org
Subject: RE: [HACKERS] What about LIMIT in SELECT ?
Date: Tue, 20 Oct 1998 17:24:09 +0900
Message-ID: <000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp> (raw)
In-Reply-To: <199810160534.BAA29942@candle.pha.pa.us>

>
> I know the good part of the posting is coming.
>
> >     Last with the btree index on key  and  the  patched  backend.
> >     This  time the plan is a plain IndexScan because the ORDER BY
> >     clause exactly matches the sort order of the  chosen  index.
> >     S1  needs  13  seconds  and  S2 less than 0.2!  This dramatic
> >     speedup comes from the fact, that this time the index scan is
> >     the  toplevel  executor  node and the executor run is stopped
> >     after 20 tuples have been selected.
>
> OK, seems like in the S1 case, the use of the psort/ORDER BY code on top
> of the index was taking and extra 3 seconds, which is 23%.  That is a
> lot more than I thought for the psort code, and shows we could gain a
> lot by removing unneeded sorts from queries that are already using
> matching indexes.
>
> Just for clarity, added to TODO.  I think everyone is clear on this one,
> and its magnitude is a surprise to me:
>
>   * Prevent psort() usage when query already using index matching ORDER BY
>
>

I can't find the reference to descending order cases except my posting.
If  we use an index scan to remove sorts in those cases,backward positioning
and scanning are necessary.

> >     Analysis of the above timings:
> >
> >     If there is an ORDER BY clause, using an index  scan  is  the
> >     clever  way  if  the  indexqual  dramatically reduces the the
> >     amount of data selected and sorted.   I  think  this  is  the
> >     normal case (who really selects nearly all rows from a 5M row
> >     table?). So choosing the index path  is  correct.  This  will
> >     hurt if someone really selects most of the rows and the index
> >     scan jumps over the disc.  But here the programmer should use
> >     an  unqualified  query  to  perform  a  seqscan  and  do  the
> >     qualification in the frontend application.
>
> Fortunately, the optimizer already does the index selection for us, and
> guesses pretty well if the index or sequential scan is better.  Once we
> implement the above removal of psort(), we will have to change the
> timings because now you have to compare index scan against sequential
> scan AND psort(), because in the index scan situation, you don't need
> the psort(), assuming the ORDER BY matches the index exactly.
>

Let t be a table with 2 indices, index1(key1,key2), index2(key1,key3).
i.e. key1 is common to index1 and index2.

And for the query
  select * from t where key1>....;

If   PosgreSQL optimizer choose [ index scan on index1 ] we can't remove
sorts from the following query.
	select * from t where key1>... order by key1,key3;

Similarly if  [ index scan on index2 ] are chosen  we can't remove sorts
from the following query.
	select * from t where key1>... order by key1,key2;

But in both cases (clever) optimizer can choose another index for scan.

Thanks.

Hiroshi Inoue
Inoue@tpf.co.jp




view thread (60+ messages)  latest in thread

Message-ID: <000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp>
Permalink:  ../000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp/
Also on:    postgresql.org/message-id/000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: Inoue@tpf.co.jp, maillist@candle.pha.pa.us, jwieck@debis.com, hackers@postgreSQL.org
  Subject: Re: [HACKERS] What about LIMIT in SELECT ?
  In-Reply-To: <000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp>
  References: <199810160534.BAA29942@candle.pha.pa.us> <000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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