Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT
Date: 2021-12-06 17:19:09
Message-ID: b79bdc27-e816-90a8-de17-3dcbf335fc73@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Francisco Olarte 2021-12-06 17:20:44 Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT
Previous Message Michael Lewis 2021-12-06 17:18:11 Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT