Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1muHOk-0000i2-Bd for pgsql-general@arkaria.postgresql.org; Mon, 06 Dec 2021 17:03:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1muHOi-0004Ln-Oh for pgsql-general@arkaria.postgresql.org; Mon, 06 Dec 2021 17:03:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1muHOi-0004Ld-5B for pgsql-general@lists.postgresql.org; Mon, 06 Dec 2021 17:03:28 +0000 Received: from outbound5f.eu.mailhop.org ([3.127.8.113]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1muHOf-0000Na-HE for pgsql-general@postgresql.org; Mon, 06 Dec 2021 17:03:27 +0000 ARC-Seal: i=1; a=rsa-sha256; t=1638810204; cv=none; d=outbound.mailhop.org; s=arc-outbound20181012; b=YTJxlVto+Ja495h+QcEa3xDV/JCTHzgxm0sdoOxl4GfczOSWwJwJmmUUsupl6S83venrs8k1dtoHW nGavyXpllCXQeqXuo5EINioDL45LaZFv7dP8SID/yOjrs6vFaOP3f94/99o7FefZLNEpCncOa9zYfa 6At3XcSXcX3C+fe4q1DtglG2pxgfNMWS3GQFowVZquEdhpLN1wZk6Ryw1eKa2fE/+7iUffb4UWFtax 0DvOTj+TZejO7Z7hT99R+bsYVMOM1qbVL2MfohiHRwF4qAb8hNzBrQG/neDZ2pnOnJF04nBCxhMr4a fyMbko1yWxkY8BN0+6nGBLDOwYRIBzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=arc-outbound20181012; h=mime-version:content-type:date:to:from:subject:message-id:dkim-signature: dkim-signature:dkim-signature:from; bh=7FfLnY733UiNJGFqg94UCBe6iWSDmd123r4Fu6zGxfY=; b=pCur3bFrKY24PPBeDy6LmO3EessW8bt6bdylGMhcfpYn/WxflhUV+Buv9qTEM7R7f/p5LbiCXq49/ 7hiDM6qvYKp+P9rdeq1oEsuxzMV6bZJh1LXJJlwP6CCtctj9khNh7SdZ+G51KJa80T5JiTwMEUVkri v03qdnuft/PxUBaZ2MZehtwlfxanQCoCax35P2Vt0LnXpHrCrAD4hBHZi5e9yMM9NcES2H3VQ2BV3m u2ExRb7A57zLi8wNWEAjiiEpdcmdd2byvHXrfNRZjEvjbE2ElVdqPgbTDXsom+8GXL2h61H09+bL2H 8B63xKFebn5EPU1FmN+kr0w41IBqmZg== ARC-Authentication-Results: i=1; outbound2.eu.mailhop.org; spf=pass smtp.mailfrom=lists.simkin.ca smtp.remote-ip=172.105.109.24; dkim=pass header.d=lists.simkin.ca header.s=athena header.a=rsa-sha256 header.b=gQAFoJ/Z; dmarc=pass header.from=lists.simkin.ca; arc=none header.oldest-pass=0; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.simkin.ca; s=duo-1610662143981-d3c4c825; h=mime-version:content-type:date:to:from:subject:message-id:from; bh=7FfLnY733UiNJGFqg94UCBe6iWSDmd123r4Fu6zGxfY=; b=X8v2vQsUge6NLDntzfQiP25UiPUrgf0x/pLaFQFdX93ZbqNK0LAgC2TYyKFsG4WkbOSLNdi5Pu7lr TkzOBZ4mTjWwoggoYI2l1YrjVGtu8HY2J8iIpucCT5WsaE6vpCyDZ2HSXBHD5iLJKc9eTwbiM5ucDH qMEO3OoO3dXbYGKM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=dkim-high; h=mime-version:content-type:date:to:from:subject:message-id:from; bh=7FfLnY733UiNJGFqg94UCBe6iWSDmd123r4Fu6zGxfY=; b=q+LxKYmxANuYr2ZvhboQKN1pFQtyYzef16Z5bVerPXACgBi1sgKPMofj6rRkYqG+6P3ObMCuNhRZG t7WX8BdG34XYscRqnRRjpzD1GqXsvvKbWPApCkWtKAoymtzg0sfSMn4qNVE52ViJwTsApAOwWH1e9n 1tik0Qk4fpHllrMo+6rO4ArhFqh5MAihIoDbUQqBp/0Hs4gsB52KBHSCgcRG0N/EQ8pQ4ZyrVMS6pl RPdpwFGB7OVO9GnkbfELf+fwstTvqWTgFv/F0ddsY9K1XYw0xEGVZV7ZXYorr6LzY2SP7UYe+AtL0Y CVX+XMHm+jVPLyU4K0D3HIjCb+gyxpA== X-Originating-IP: 172.105.109.24 X-MHO-RoutePath: c2lta2lu X-MHO-User: 6a4477a6-56b6-11ec-a073-973b52397bcb X-Report-Abuse-To: https://support.duocircle.com/support/solutions/articles/5000540958-duocircle-standard-smtp-abuse-information X-Mail-Handler: DuoCircle Outbound SMTP Received: from athena.simkin.ca (athena.simkin.ca [172.105.109.24]) by outbound2.eu.mailhop.org (Halon) with ESMTPSA id 6a4477a6-56b6-11ec-a073-973b52397bcb; Mon, 06 Dec 2021 17:03:22 +0000 (UTC) Received: from skynet.simkin.ca (node-1w7jr9sr73xr4ze19xwgjcteu.ipv6.telus.net [IPv6:2001:569:bce0:6400:fa75:a4ff:fe2f:92e6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by athena.simkin.ca (Postfix) with ESMTPSA id 265F7C6A13DB for ; Mon, 6 Dec 2021 09:03:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lists.simkin.ca; s=athena; t=1638810185; bh=vBhXT6K6qrk2dWEl/7IkEBiExxZmwiY3IGHEJRkOazI=; h=Subject:From:To:Date:From:To:Subject:Date:Reply-To; b=gQAFoJ/ZjW3F3Oy5G1CCskNDIVXjrhlXnPRyqViAvbHRGdtqxgauyQ60HNWZjAoX8 Vm9ATJ3cS10wA+8lR8BwgLaugkUZScePWEHtENlt1iEbSykvnRSMCgJOT19mjon9wZ Ca7Dmlmotr84ZpeUzHFr6MvwUAvqW8Z6Fx9zhnHtLdHyW96t42hwxzTr+egCeJsZyA EcrzX5h5GQvwaXb6a0f8rzkW8p57pZIkimfAUKMRGXUWltZS7cTKwf6EHI7o8rRI1Y FVGY6C7KRAl2eFk0RF6HsfFrLyhybUj1+QG/1DDg76gRhkhfh2+Sa2ZNgg/XAQf4xJ E6hBx+GEamrTA== Received: from skynet.simkin.ca (skynet.simkin.ca [IPv6:::1]) by skynet.simkin.ca (Postfix) with ESMTP id 509F7105B1B00 for ; Mon, 6 Dec 2021 09:03:02 -0800 (PST) Message-ID: Subject: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT From: Alan Hodgson To: Postgres General Date: Mon, 06 Dec 2021 09:03:02 -0800 Content-Type: multipart/alternative; boundary="=-StkOo4o7B+mLjDk5LZE3" X-PGP-Key: fp="EF82 6F8D A3BA A096 FFA3 05F2 A7A0 40A5 34E5 AA0B"; id="0x34E5AA0B"; get= User-Agent: Evolution 3.40.4 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-StkOo4o7B+mLjDk5LZE3 Content-Type: text/plain; charset="ISO-8859-15" Content-Transfer-Encoding: 8bit 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. The table has nearly 29 million records. 5069 of them match shipment_import_id = 5090609. There is an index on shipment_import_id, which the planner happily uses without the LIMIT specifically. Yet with it the query planner will always do something like: # 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. I know I can work around this. Generally I would just drop the index on shipment_import_id and create one on shipment_import_id,id. Or if I can get the devs to wrap their query in an inner select with a fake offset to fool the query planner that works too. But both seem hacky. Just wondering if there's a knob I can turn to make these more likely to work without constantly implementing workarounds? Thanks for any help. --=-StkOo4o7B+mLjDk5LZE3 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
I keep running into problems l= ike these:

Devs are using an ORM. It really likes = to produce queries like:

SELECT "shipment_import_r= ecords".* FROM "shipment_import_records" WHERE shipment_import_records"."sh= ipment_import_id" =3D 5090609 ORDER BY "shipment_import_records"."id" ASC L= IMIT 1;

I don't know why they do this. Usually it'= s more like 50 for pagination which make more sense. But for whatever reaso= n this keeps coming up.

The table has nearly 29 mi= llion records. 5069 of them match shipment_import_id =3D 5090609. Ther= e is an index on shipment_import_id, which the planner happily uses wi= thout the LIMIT specifically. Yet with it the query planner will always do = something like:

# explain= SELECT "shipment_import_records".* FROM "shipment_import_records" WHERE "s= hipment_import_records"."shipment_import_id" =3D 5090609 ORDER BY "shipment= _import_records"."id" ASC LIMIT 1;       &nbs= p; 
          &n= bsp;            = ;            &n= bsp;            = ;        QUERY PLAN   &nb= sp;            =             &nb= sp;            =             &nb= sp;     
--------------------------------------= ---------------------------------------------------------------------------= ------------
Limit  (cost=3D0.44..873.35 rows=3D1 width=3D243)
  ->  Index Scan using shipment_import_records_pkey = on shipment_import_records  (cost=3D0.44..5122227.70 rows=3D5868 width= =3D243)
        Filter: (shipment_impo= rt_id =3D 5090609)
<= br>
.. which takes minutes.

I know I can work around this. Generally I would just drop = the index on shipment_import_id and create one on shipment_import_id,id. Or= if I can get the devs to wrap their query in an inner select with a fake o= ffset to fool the query planner that works too. But both seem hacky.
<= div>
Just wondering if there's a knob I can turn to make thes= e more likely to work without constantly implementing workarounds?

Thanks for any help.
<= /html> --=-StkOo4o7B+mLjDk5LZE3--