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 1o49g1-0006YK-Q4 for pgsql-general@arkaria.postgresql.org; Wed, 22 Jun 2022 23:22:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o49g0-0008Lw-Mi for pgsql-general@arkaria.postgresql.org; Wed, 22 Jun 2022 23:22:24 +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 1o49g0-0008Ln-Bx for pgsql-general@lists.postgresql.org; Wed, 22 Jun 2022 23:22:24 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o49fy-0002UH-85 for pgsql-general@lists.postgresql.org; Wed, 22 Jun 2022 23:22:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 25MNMDwK3695493; Wed, 22 Jun 2022 19:22:13 -0400 From: Tom Lane To: "Peter J. Holzer" cc: "Dirschel, Steve" , pgsql-general@lists.postgresql.org Subject: Re: Tuning a query with ORDER BY and LIMIT In-reply-to: <20220622221321.vjrapfkid3rprun5@hjp.at> References: <20220622221321.vjrapfkid3rprun5@hjp.at> Comments: In-reply-to "Peter J. Holzer" message dated "Thu, 23 Jun 2022 00:13:21 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3695491.1655940133.1@sss.pgh.pa.us> Date: Wed, 22 Jun 2022 19:22:13 -0400 Message-ID: <3695492.1655940133@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Peter J. Holzer" writes: > On 2022-06-22 19:39:33 +0000, Dirschel, Steve wrote: >> create index workflow_execution_initial_ui_tabs >> on workflow_execution (workflow_id asc, status asc, result asc, >> completed_datetime desc); > [...] >> explain (analyze, verbose, costs, buffers, timing, summary, hashes) >> select * from workflow_execution >> where workflow_id = 14560 and >> status = 'COMPLETED' and >> result = 'SUCCEEDED' >> order by completed_datetime desc limit 50; > This query should actually be able to use the index, since the first > columns in the index are all compared to single values. So the you can > just jump to the first matching index and then get the next 50 entries. Yeah, that sure looks like it should work. Since we've not been shown the whole table's DDL, it's hard to guess at why it isn't; maybe there's a data type issue? >> Is Postgres unable to optimize the query similar to Oracle? Is it possible >> this is possible but we are running on too old of a version? > PostgreSQL 10 is quite old, so that's a possibility. That's worked for many ages. regards, tom lane