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 1o5AYP-0003Bo-Ne for pgsql-general@arkaria.postgresql.org; Sat, 25 Jun 2022 18:30:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o5AYM-0008Sg-Q7 for pgsql-general@arkaria.postgresql.org; Sat, 25 Jun 2022 18:30:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o5AYM-0008Pp-FP for pgsql-general@lists.postgresql.org; Sat, 25 Jun 2022 18:30:42 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o5AYI-00033o-01 for pgsql-general@lists.postgresql.org; Sat, 25 Jun 2022 18:30:41 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 280AB72ED; Sat, 25 Jun 2022 20:30:36 +0200 (CEST) Date: Sat, 25 Jun 2022 20:30:36 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Tuning a query with ORDER BY and LIMIT Message-ID: <20220625183036.rccznhompi323c4r@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: <20220622214837.c2wt734ywfzvpvav@hjp.at> <20220622221919.pnm7hgu5r7rxfwae@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="qvpslkykmjgb5mb2" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --qvpslkykmjgb5mb2 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2022-06-22 23:10:25 -0400, Jeff Janes wrote: > On Wed, Jun 22, 2022 at 6:19 PM Peter J. Holzer 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 new= est > >entries and retrieve only those from the table. That should be faster > >since the index contains only 4 of 28 (if I counted correctly) colum= ns > >and should be quite a bit smaller. >=20 > Another - better - optimization would be to fetch the first 50 results > for each of the 6 possible values of result, then choose the 50 large= st > of those. That sounds tricky to generalize, though. >=20 >=20 > You don't even need to read 50 from each of the 6 branches.=A0 If you use= a merge > append operation, you would need to read=A0 55 rows.=A0 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 --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --qvpslkykmjgb5mb2 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmK3VEQACgkQ8g5IURL+ KF3eXRAAk5mK1BrsUNhlRoODuEHP4aJToYjU5hdxpySXyZUry9+/zmETaGKuRu/p xY/l7fpnrIJKHtUDeJBO+NyKQrEwxH4MJrmcBi10jlIKWUiiGt7uYVUsbEoujc89 xgYGa8Tfj5swac55g5xqrdW0UeaZaTlM6PZhoyNbKoDrGFpX/sp3Ysijk5du9X2A M6wwG+pv+CJP7y1NLWeAGgrUYnRVR8KFd5C6+Qj9wHxN1UajY/gsfkFbduAYf0rn KrVsMnvXUK2Oa89zpXue8KmuKHnGMl1QbaMP3Wa38fknPR9twH9wDfP9i+0ZI9eQ USE5VX1qJdO21v7wyvQ1Z6dPkVscR+NgWpUkQwGMmrE0Sgzx/SiWslY6XJ93C5SG U3YTc83RtnFNF8q2j95HpdhJ94mM6jVwK22WkmKwFz8UfJggpINuQzIqYHOku9+F Tv5WgNe0pzeSP3kNadoTNPZNfI9TVnHILTLxVXPZ4xiXKY/FElNf3cOWwO0H4aSC bz3UxBRubRcOBMLydPqXTvUnGvxtQrTZgrrsXzY8laYR4AyAAgmJg5xES0x/I89O 18zVSavf6fGnH4GNRaqlsyg9rSL8Lq76hU2sCfUM4FRhGImOAJVcZx3Paaayv95m 7tEDuYep415jhWlkMKpn/gNAQcXuy6pL7BgEB5exaGoz1Gksx8U= =2jWy -----END PGP SIGNATURE----- --qvpslkykmjgb5mb2--