pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT
Date: Mon, 6 Dec 2021 10:19:09 -0700
Message-ID: <b79bdc27-e816-90a8-de17-3dcbf335fc73@gmail.com> (raw)
In-Reply-To: <bc47e9d0c490942eebb01dc1129097012005e1d2.camel@lists.simkin.ca>
References: <bc47e9d0c490942eebb01dc1129097012005e1d2.camel@lists.simkin.ca>

On 12/6/21 10:03 AM, Alan Hodgson wrote:
> I keep running into problems like these:
>
> Devs are using an ORM. It really likes to produce queries like:
>
> 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;
>
> I don't know why they do this. Usually it's more like 50 for 
> pagination which make more sense. But for whatever reason this keeps 
> coming up.
>
To be clear, is it the devs or the ORM that's adding the ORDER  and the 
LIMIT?  I'm betting on devs.  Do they need the smallest id (first 
occurrance?) or do they need data common to all 5096 entries (Name?) and 
any record will do?.  For the former they might be better off asking for 
just the attributes they need and for the latter you need to provide an 
option which gets them that single record.  Of course, If they have the 
"smallest id" in hand they should request that.






view thread (13+ messages)  latest in thread

Message-ID: <b79bdc27-e816-90a8-de17-3dcbf335fc73@gmail.com>
Permalink:  ../b79bdc27-e816-90a8-de17-3dcbf335fc73@gmail.com/
Also on:    postgresql.org/message-id/b79bdc27-e816-90a8-de17-3dcbf335fc73@gmail.com

 · 

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: robjsargent@gmail.com
  Subject: Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT
  In-Reply-To: <b79bdc27-e816-90a8-de17-3dcbf335fc73@gmail.com>
  References: <bc47e9d0c490942eebb01dc1129097012005e1d2.camel@lists.simkin.ca> <b79bdc27-e816-90a8-de17-3dcbf335fc73@gmail.com>

* 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