pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Tuning a query with ORDER BY and LIMIT
Date: Sat, 25 Jun 2022 20:30:36 +0200
Message-ID: <20220625183036.rccznhompi323c4r@hjp.at> (raw)
In-Reply-To: <CAMkU=1yqjCviD4OByQpXeHZn9TE+uvZj0Mpm2uUumsp5+rK_Bg@mail.gmail.com>
References: <DM6PR03MB4332A41B10218AE854D63A72FAB29@DM6PR03MB4332.namprd03.prod.outlook.com>
	<20220622214837.c2wt734ywfzvpvav@hjp.at>
	<20220622221919.pnm7hgu5r7rxfwae@hjp.at>
	<CAMkU=1yqjCviD4OByQpXeHZn9TE+uvZj0Mpm2uUumsp5+rK_Bg@mail.gmail.com>

On 2022-06-22 23:10:25 -0400, Jeff Janes wrote:
> On Wed, Jun 22, 2022 at 6:19 PM Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
>     >That's just how btree indexes work and Oracle will have the same
>     >limitation. What would be possible is to use an index only scan
>     >(returning 2,634,718 matching results), sort that to find the 50 newest
>     >entries and retrieve only those from the table. That should be faster
>     >since the index contains only 4 of 28 (if I counted correctly) columns
>     >and should be quite a bit smaller.
> 
>     Another - better - optimization would be to fetch the first 50 results
>     for each of the 6 possible values of result, then choose the 50 largest
>     of those. That sounds tricky to generalize, though.
> 
> 
> You don't even need to read 50 from each of the 6 branches.  If you use a merge
> append operation, you would need to read  55 rows.  50 to be returned, and one
> non-returned from each branch other than the one returning the last row.

Yes, but that means a lot of jumping around in the index.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (10+ messages)

Message-ID: <20220625183036.rccznhompi323c4r@hjp.at>
Permalink:  ../20220625183036.rccznhompi323c4r@hjp.at/
Also on:    postgresql.org/message-id/20220625183036.rccznhompi323c4r@hjp.at

 · 

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-general@postgresql.org
  Cc: hjp-pgsql@hjp.at
  Subject: Re: Tuning a query with ORDER BY and LIMIT
  In-Reply-To: <20220625183036.rccznhompi323c4r@hjp.at>
  References: <DM6PR03MB4332A41B10218AE854D63A72FAB29@DM6PR03MB4332.namprd03.prod.outlook.com> <20220622214837.c2wt734ywfzvpvav@hjp.at> <20220622221919.pnm7hgu5r7rxfwae@hjp.at> <CAMkU=1yqjCviD4OByQpXeHZn9TE+uvZj0Mpm2uUumsp5+rK_Bg@mail.gmail.com> <20220625183036.rccznhompi323c4r@hjp.at>

* 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