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 1o46Yl-0007oM-Fu for pgsql-general@arkaria.postgresql.org; Wed, 22 Jun 2022 20:02:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o46Yj-0002jx-OG for pgsql-general@arkaria.postgresql.org; Wed, 22 Jun 2022 20:02:41 +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 1o46Yj-0002jo-9Y for pgsql-general@lists.postgresql.org; Wed, 22 Jun 2022 20:02:41 +0000 Received: from mail-oa1-x29.google.com ([2001:4860:4864:20::29]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o46Yg-0000t0-Bz for pgsql-general@lists.postgresql.org; Wed, 22 Jun 2022 20:02:40 +0000 Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-101e1a33fe3so13641360fac.11 for ; Wed, 22 Jun 2022 13:02:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=f4vp82NYUbzl7be8nAjnSLGTvePbt+mh80eLBTbLT+M=; b=kpzef2Z7TInsq+Yj4O3JJAI6RV3F0CKW2A7AfEMQakovBz959AzPAcqnBWhmyWX3oc a2psrbaS1Wd1RR5NuA2k2+bNLFUhcgxGoS1so0glLCvCof7uHnP4V/hCwio7u3xBbCPb fVuDLE/rIiPmNj4lqlJ3h4LosUFCX1dNWPwthVguPXoIqdXiN0f2pUSGKweK2zbRglsQ xQT6vySfVXil92kkXgTXrns8wvno+S7qcS2CCZb4JGm9JBKpNJ0LCn9R6loqAiQXomV3 N1GDzmmbkw8TTgBhEgi7dAEAuu2+KtyOFHlstijD5hajQDP+sjJjF7MGTLrgJC1CG0t7 iDqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=f4vp82NYUbzl7be8nAjnSLGTvePbt+mh80eLBTbLT+M=; b=Ph5L3Dj2004nYDQmcB391fPO7utvtqj2fAf+R4JpIZrNsYdmCgg0nwARMakfzR401a DNKcZSGNuYnWDS8w2EGCVhngM7ywIhoxU6wrWegsmxZHwHznjP75KfZ7WT5WhkkuQHIu UTqxFo5Jx+xqG0UKl3p5TitcE2H0flkmxLbqZBoejA/3ZhDpvOL0jL/nc03Zmy1z9oYH 0lzzcl6VOLbcd8DZ8X41IUe03PalRPKVcOe3VMUhf4OWhddBdxX6p+IK66rsx4xAfBpR lHQovYklfDYUGZ6D4IJhpuGQa3wVkgdOGvLP8r2yw+yP64nLMU60c8eqhaE8WpJKUyUc P0CA== X-Gm-Message-State: AJIora+5S+G7h9X1LONCIctR/g8p55i03bywOT4/fZFglxFbHaFuxHTQ nW9ifuEand9apVrNLegp1yFrz13SGsjRE9q11Ds= X-Google-Smtp-Source: AGRyM1tBWGvYfS2cFYFZgtqwy8mTMXyg/jZ0b1RoAH776b+NrgZv6Ux1RpaUuRkeoux59eZgKX1xzxw76TsypW6+hBg= X-Received: by 2002:a05:6870:f2a7:b0:f1:7205:d130 with SMTP id u39-20020a056870f2a700b000f17205d130mr61797oap.146.1655928156578; Wed, 22 Jun 2022 13:02:36 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Michael van der Kolff Date: Thu, 23 Jun 2022 06:02:25 +1000 Message-ID: Subject: Re: Tuning a query with ORDER BY and LIMIT To: "Dirschel, Steve" Cc: "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="0000000000008be83a05e20ed10b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000008be83a05e20ed10b Content-Type: text/plain; charset="UTF-8" What do you see when you remove the LIMIT clause? It may be possible to rewrite this using ROW_NUMBER. --Michael On Thu, Jun 23, 2022 at 5:39 AM Dirschel, Steve < steve.dirschel@thomsonreuters.com> wrote: > I am fairly new to tuning Postgres queries. I have a long background > tuning Oracle queries. > > > > Posrgres version 10.11 > > > > Here is the DDL for the index the query is using: > > > > 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 in > ('SUCCEEDED','REEXECUTED','ABORTED','DISCONTINUED','FAILED','PARTIAL_SUCCESS') > > order by completed_datetime desc limit 50; > > > > -- > > Limit (cost=56394.91..56395.04 rows=50 width=1676) (actual > time=3400.608..3400.622 rows=50 loops=1) > > " Output: execution_id, state_machine_id, workflow_id, started_datetime, > completed_datetime, status, execution_context_s3_arn, ol_version, > created_datetime, updated_datetime, deleted_millis, acquisition_channel_id, > correlation_id, result, state_machine_execution_arn, created_by_id, > updated_by_id, acquired_gcs_s3_object, sqs_trigger_id, trigger_message, > acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigger_id, > notification_trigger_workflow_id, acquired_object_name, subscription_guid" > > Buffers: shared hit=142368 > > -> Sort (cost=56394.91..56432.71 rows=15118 width=1676) (actual > time=3400.607..3400.615 rows=50 loops=1) > > " Output: execution_id, state_machine_id, workflow_id, > started_datetime, completed_datetime, status, execution_context_s3_arn, > ol_version, created_datetime, updated_datetime, deleted_millis, > acquisition_channel_id, correlation_id, result, > state_machine_execution_arn, created_by_id, updated_by_id, > acquired_gcs_s3_object, sqs_trigger_id, trigger_message, > acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigger_id, > notification_trigger_workflow_id, acquired_object_name, subscription_guid" > > Sort Key: workflow_execution.completed_datetime DESC > > Sort Method: top-N heapsort Memory: 125kB > > Buffers: shared hit=142368 > > -> Index Scan using workflow_execution_initial_ui_tabs on > workflow.workflow_execution (cost=0.69..55892.70 rows=15118 width=1676) > (actual time=0.038..2258.579 rows=2634718 loops=1) > > " Output: execution_id, state_machine_id, workflow_id, > started_datetime, completed_datetime, status, execution_context_s3_arn, > ol_version, created_datetime, updated_datetime, deleted_millis, > acquisition_channel_id, correlation_id, result, > state_machine_execution_arn, created_by_id, updated_by_id, > acquired_gcs_s3_object, sqs_trigger_id, trigger_message, > acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigger_id, > notification_trigger_workflow_id, acquired_object_name, subscription_guid" > > " Index Cond: ((workflow_execution.workflow_id = 14560) AND > ((workflow_execution.status)::text = 'COMPLETED'::text) AND > ((workflow_execution.result)::text = ANY > ('{SUCCEEDED,REEXECUTED,ABORTED,DISCONTINUED,FAILED,PARTIAL_SUCCESS}'::text[])))" > > Buffers: shared hit=142368 > > Planning time: 0.217 ms > > Execution time: 3400.656 ms > > > > With Oracle for a query like this since the index is on the 3 columns > matching the WHERE clause and the ORDER BY clause is in the 4th position > Oracle would be able to scan that index and as soon as it finds the first > matching 50 rows. But as you can see above Postgres is finding 2,634,718 > matching rows for the WHERE clause , sorts them, and then returns the first > 50 rows. > > > > I was questioning if the result IN clause was causing the issue so I ran > the query with result = and see the same results: > > > > 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; > > > > Limit (cost=54268.09..54268.22 rows=50 width=1676) (actual > time=3372.453..3372.467 rows=50 loops=1) > > " Output: execution_id, state_machine_id, workflow_id, started_datetime, > completed_datetime, status, execution_context_s3_arn, ol_version, > created_datetime, updated_datetime, deleted_millis, acquisition_channel_id, > correlation_id, result, state_machine_execution_arn, created_by_id, > updated_by_id, acquired_gcs_s3_object, sqs_trigger_id, trigger_message, > acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigger_id, > notification_trigger_workflow_id, acquired_object_name, subscription_guid" > > Buffers: shared hit=140313 > > -> Sort (cost=54268.09..54304.46 rows=14547 width=1676) (actual > time=3372.452..3372.460 rows=50 loops=1) > > " Output: execution_id, state_machine_id, workflow_id, > started_datetime, completed_datetime, status, execution_context_s3_arn, > ol_version, created_datetime, updated_datetime, deleted_millis, > acquisition_channel_id, correlation_id, result, > state_machine_execution_arn, created_by_id, updated_by_id, > acquired_gcs_s3_object, sqs_trigger_id, trigger_message, > acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigger_id, > notification_trigger_workflow_id, acquired_object_name, subscription_guid" > > Sort Key: workflow_execution.completed_datetime DESC > > Sort Method: top-N heapsort Memory: 125kB > > Buffers: shared hit=140313 > > -> Index Scan using workflow_execution_initial_ui_tabs on > workflow.workflow_execution (cost=0.69..53784.85 rows=14547 width=1676) > (actual time=0.034..2238.867 rows=2616747 loops=1) > > " Output: execution_id, state_machine_id, workflow_id, > started_datetime, completed_datetime, status, execution_context_s3_arn, > ol_version, created_datetime, updated_datetime, deleted_millis, > acquisition_channel_id, correlation_id, result, > state_machine_execution_arn, created_by_id, updated_by_id, > acquired_gcs_s3_object, sqs_trigger_id, trigger_message, > acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigger_id, > notification_trigger_workflow_id, acquired_object_name, subscription_guid" > > Index Cond: ((workflow_execution.workflow_id = 14560) AND > ((workflow_execution.status)::text = 'COMPLETED'::text) AND > ((workflow_execution.result)::text = 'SUCCEEDED'::text)) > > Buffers: shared hit=140313 > > Planning time: 0.264 ms > > Execution time: 3372.511 ms > > > > > > 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? > > > > Thanks in advance for any input. > > > > Steve > > > > > This e-mail is for the sole use of the intended recipient and contains > information that may be privileged and/or confidential. If you are not an > intended recipient, please notify the sender by return e-mail and delete > this e-mail and any attachments. Certain required legal entity disclosures > can be accessed on our website: > https://www.thomsonreuters.com/en/resources/disclosures.html > --0000000000008be83a05e20ed10b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
What do you see when you remove the LIMIT clause? It may b= e possible to rewrite this using ROW_NUMBER.

--Michael

On Thu, Jun 23, 2022 at 5:39 AM Dirschel, Steve <steve.dirschel@thomsonreuters.com>= wrote:

I am fairly new to tuning Postgres queries.=C2=A0 I = have a long background tuning Oracle queries.

=C2=A0

Posrgres version 10.11

=C2=A0

Here is the DDL for the index the query is using:=

=C2=A0

create index workflow_execution_initial_ui_tabs=

=C2=A0=C2=A0=C2=A0 on workflow_execution (workflow_id asc, st= atus asc, result asc, completed_datetime desc);

=C2=A0

=C2=A0

explain (analyze, verbose, costs, buffers, timing, summary, h= ashes)

select * from workflow_execution

where workflow_id =3D 14560 and

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 status =3D 'COMPLETED'= and

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 result in ('SUCCEEDED'= ,'REEXECUTED','ABORTED','DISCONTINUED','FAILED&= #39;,'PARTIAL_SUCCESS')

order by completed_datetime desc limit 50;

=C2=A0

--

Limit=C2=A0 (cost=3D56394.91..56395.04 rows=3D50 width=3D1676= ) (actual time=3D3400.608..3400.622 rows=3D50 loops=3D1)

"=C2=A0 Output: execution_id, state_machine_id, workflow= _id, started_datetime, completed_datetime, status, execution_context_s3_arn= , ol_version, created_datetime, updated_datetime, deleted_millis, acquisition_channel_id, correlation_id, result, state_machine_execution_ar= n, created_by_id, updated_by_id, acquired_gcs_s3_object, sqs_trigger_id, tr= igger_message, acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigg= er_id, notification_trigger_workflow_id, acquired_object_name, subscription_guid"

=C2=A0 Buffers: shared hit=3D142368

=C2=A0 ->=C2=A0 Sort=C2=A0 (cost=3D56394.91..56432.71 rows= =3D15118 width=3D1676) (actual time=3D3400.607..3400.615 rows=3D50 loops=3D= 1)

"=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Output: exec= ution_id, state_machine_id, workflow_id, started_datetime, completed_dateti= me, status, execution_context_s3_arn, ol_version, created_datetime, updated= _datetime, deleted_millis, acquisition_channel_id, correlation_id, result, state_mach= ine_execution_arn, created_by_id, updated_by_id, acquired_gcs_s3_object, sq= s_trigger_id, trigger_message, acquired_gcs_s3_object_uuid, api_trigger_id,= scheduled_trigger_id, notification_trigger_workflow_id, acquired_object_name, subscription_guid"

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Sort Key: workflow= _execution.completed_datetime DESC

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Sort Method: top-N= heapsort=C2=A0 Memory: 125kB

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Buffers: shared hi= t=3D142368

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ->=C2=A0 Index = Scan using workflow_execution_initial_ui_tabs on workflow.workflow_executio= n=C2=A0 (cost=3D0.69..55892.70 rows=3D15118 width=3D1676) (actual time=3D0.= 038..2258.579 rows=3D2634718 loops=3D1)

"=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 Output: execution_id, state_machine_id, workflow_i= d, started_datetime, completed_datetime, status, execution_context_s3_arn, = ol_version, created_datetime, updated_datetime, deleted_millis, acquisition_channel_id, correlation_id, result, state_mach= ine_execution_arn, created_by_id, updated_by_id, acquired_gcs_s3_object, sq= s_trigger_id, trigger_message, acquired_gcs_s3_object_uuid, api_trigger_id,= scheduled_trigger_id, notification_trigger_workflow_id, acquired_object_name, subscription_guid"

"=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 Index Cond: ((workflow_execution.workflow_id =3D 1= 4560) AND ((workflow_execution.status)::text =3D 'COMPLETED'::text)= AND ((workflow_execution.result)::text =3D ANY ('{SUCCEEDED,REEXECUTED= ,ABORTED,DISCONTINUED,FAILED,PARTIAL_SUCCESS}'::text[])))"<= u>

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 Buffers: shared hit=3D142368

Planning time: 0.217 ms

Execution time: 3400.656 ms

=C2=A0

With Oracle for a query like this since the index is= on the 3 columns matching the WHERE clause and the ORDER BY clause is in t= he 4th position Oracle would be able to scan that index and as s= oon as it finds the first matching 50 rows.=C2=A0 But as you can see above Postgres is finding 2,634,718 matc= hing rows for the WHERE clause , sorts them, and then returns the first 50 = rows.=C2=A0 =C2=A0

=C2=A0

I was questioning if the result IN clause was causin= g the issue so I ran the query with result =3D and see the same results:=

=C2=A0

explain (analyze, verbose, costs, buffers, timing, summary, h= ashes)

select * from workflow_execution

where workflow_id =3D 14560 and

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 status =3D 'COMPLETED'= and

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 result =3D 'SUCCEEDED'=

order by completed_datetime desc limit 50;

=C2=A0

Limit=C2=A0 (cost=3D54268.09..54268.22 rows=3D50 width=3D1676= ) (actual time=3D3372.453..3372.467 rows=3D50 loops=3D1)

"=C2=A0 Output: execution_id, state_machine_id, workflow= _id, started_datetime, completed_datetime, status, execution_context_s3_arn= , ol_version, created_datetime, updated_datetime, deleted_millis, acquisition_channel_id, correlation_id, result, state_machine_execution_ar= n, created_by_id, updated_by_id, acquired_gcs_s3_object, sqs_trigger_id, tr= igger_message, acquired_gcs_s3_object_uuid, api_trigger_id, scheduled_trigg= er_id, notification_trigger_workflow_id, acquired_object_name, subscription_guid"

=C2=A0 Buffers: shared hit=3D140313

=C2=A0 ->=C2=A0 Sort=C2=A0 (cost=3D54268.09..54304.46 rows= =3D14547 width=3D1676) (actual time=3D3372.452..3372.460 rows=3D50 loops=3D= 1)

"=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Output: exec= ution_id, state_machine_id, workflow_id, started_datetime, completed_dateti= me, status, execution_context_s3_arn, ol_version, created_datetime, updated= _datetime, deleted_millis, acquisition_channel_id, correlation_id, result, state_mach= ine_execution_arn, created_by_id, updated_by_id, acquired_gcs_s3_object, sq= s_trigger_id, trigger_message, acquired_gcs_s3_object_uuid, api_trigger_id,= scheduled_trigger_id, notification_trigger_workflow_id, acquired_object_name, subscription_guid"

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Sort Key: workflow= _execution.completed_datetime DESC

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Sort Method: top-N= heapsort=C2=A0 Memory: 125kB

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Buffers: shared hi= t=3D140313

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ->=C2=A0 Index = Scan using workflow_execution_initial_ui_tabs on workflow.workflow_executio= n=C2=A0 (cost=3D0.69..53784.85 rows=3D14547 width=3D1676) (actual time=3D0.= 034..2238.867 rows=3D2616747 loops=3D1)

"=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 Output: execution_id, state_machine_id, workflow_i= d, started_datetime, completed_datetime, status, execution_context_s3_arn, = ol_version, created_datetime, updated_datetime, deleted_millis, acquisition_channel_id, correlation_id, result, state_mach= ine_execution_arn, created_by_id, updated_by_id, acquired_gcs_s3_object, sq= s_trigger_id, trigger_message, acquired_gcs_s3_object_uuid, api_trigger_id,= scheduled_trigger_id, notification_trigger_workflow_id, acquired_object_name, subscription_guid"

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 Index Cond: ((workflow_execution.workflow_id =3D 14560) = AND ((workflow_execution.status)::text =3D 'COMPLETED'::text) AND (= (workflow_execution.result)::text =3D 'SUCCEEDED'::text))=

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 Buffers: shared hit=3D140313

Planning time: 0.264 ms

Execution time: 3372.511 ms

=C2=A0

=C2=A0

Is Postgres unable to optimize the query similar to = Oracle?=C2=A0 Is it possible this is possible but we are running on too old= of a version?

=C2=A0

Thanks in advance for any input.

=C2=A0

Steve

=C2=A0

=C2=A0

This e-mail is for the sole use of the intended recipient and contains info= rmation that may be privileged and/or confidential. If you are not an inten= ded recipient, please notify the sender by return e-mail and delete this e-= mail and any attachments. Certain required legal entity disclosures can be accessed on our website: https://www.thomsonreuters.com/en/resources/disclosures.html
--0000000000008be83a05e20ed10b--