pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Pavel Stehule <pavel.stehule@gmail.com>
To: Francisco Olarte <folarte@peoplecall.com>
Cc: Alan Hodgson <ahodgson@lists.simkin.ca>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT
Date: Tue, 07 Dec 2021 04:16:27 +0100
Message-ID: <82b3a0d97f5ee6279f1c7e6048c510b07f0b313b.camel@cybertec.at> (raw)
In-Reply-To: <CAFj8pRDsmF2PVKOQK47EhOfhSPrKuRoUSfb=cwJJSM+NubbUMg@mail.gmail.com>
References: <bc47e9d0c490942eebb01dc1129097012005e1d2.camel@lists.simkin.ca>
	<CA+bJJbxT55278A6B3vUKHgNR-aSDd6YWbSvKb_KMNO9c6ZEbGQ@mail.gmail.com>
	<CAFj8pRDsmF2PVKOQK47EhOfhSPrKuRoUSfb=cwJJSM+NubbUMg@mail.gmail.com>

On Mon, 2021-12-06 at 19:22 +0100, Pavel Stehule wrote:

> po 6. 12. 2021 v 18:21 odesílatel Francisco Olarte <folarte@peoplecall.com> napsal:
> > On Mon, 6 Dec 2021 at 18:03, Alan Hodgson <ahodgson@lists.simkin.ca> wrote:
> > > # explain SELECT "shipment_import_records".* FROM "shipment_import_records" WHERE "shipment_import_records"."shipment_import_id" = 5090609 ORDER BY "shipment_import_records"."id" ASC LIMIT 1;
> > >                                                          QUERY PLAN
> > > -----------------------------------------------------------------------------------------------------------------------------
> > > Limit  (cost=0.44..873.35 rows=1 width=243)
> > >    ->  Index Scan using shipment_import_records_pkey on shipment_import_records  (cost=0.44..5122227.70 rows=5868 width=243)
> > >          Filter: (shipment_import_id = 5090609)
> > > .. which takes minutes.
> > >
> > > Just wondering if there's a knob I can turn to make these more likely to work without constantly implementing workarounds?
> > 
> > You may try a composite index.
>
> +1 These issues can be solved by composite indexes. The low limit clause deforms costs and when the data are not really random, then index scan can be too long.

An ugly alternative is to use "ORDER BY id + 0", which prevents PostgreSQL
from using the index.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






view thread (13+ messages)  latest in thread

Message-ID: <82b3a0d97f5ee6279f1c7e6048c510b07f0b313b.camel@cybertec.at>
Permalink:  ../82b3a0d97f5ee6279f1c7e6048c510b07f0b313b.camel@cybertec.at/
Also on:    postgresql.org/message-id/82b3a0d97f5ee6279f1c7e6048c510b07f0b313b.camel@cybertec.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: laurenz.albe@cybertec.at, pavel.stehule@gmail.com, folarte@peoplecall.com, ahodgson@lists.simkin.ca
  Subject: Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT
  In-Reply-To: <82b3a0d97f5ee6279f1c7e6048c510b07f0b313b.camel@cybertec.at>
  References: <bc47e9d0c490942eebb01dc1129097012005e1d2.camel@lists.simkin.ca> <CA+bJJbxT55278A6B3vUKHgNR-aSDd6YWbSvKb_KMNO9c6ZEbGQ@mail.gmail.com> <CAFj8pRDsmF2PVKOQK47EhOfhSPrKuRoUSfb=cwJJSM+NubbUMg@mail.gmail.com> <82b3a0d97f5ee6279f1c7e6048c510b07f0b313b.camel@cybertec.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