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 1nfaCa-00082Z-C0 for pgsql-admin@arkaria.postgresql.org; Sat, 16 Apr 2022 04:38:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nfaBb-0001fb-35 for pgsql-admin@arkaria.postgresql.org; Sat, 16 Apr 2022 04:37:27 +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 1nfaBa-0001fS-Mz for pgsql-admin@lists.postgresql.org; Sat, 16 Apr 2022 04:37:26 +0000 Received: from mail-yb1-xb2b.google.com ([2607:f8b0:4864:20::b2b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nfaBY-00085k-Dm for pgsql-admin@lists.postgresql.org; Sat, 16 Apr 2022 04:37:26 +0000 Received: by mail-yb1-xb2b.google.com with SMTP id h8so17271968ybj.11 for ; Fri, 15 Apr 2022 21:37:24 -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=w8uSapcKgfMjO1Om8tjS5wxWIbfF15rTJMhijIOGqBw=; b=nTrwy29scM3oA4oDFTxymAlfrkguJAz/fEEnrlppTqEwT+aHV8cCVIZ7BbeuOG6mB9 KTCqAKZzrJoXgI0RdnRUDf6E9CT2KlUdgSaQf0iCk8u43NhJU3yzOOGa4Od2bRl/WG8c NkyE/T7GH7CFm7eE3f3BRpQW2Oog6mLC9NNBp/3afgr1wfJ6v6T3ZIySN+3UaeEvrx7h jH1V6+vKLOmIoYq8CGAOt/fG2e/6ZSx/TK5AhHsgMObRRuQ+S6p6/E01gyNDTfA42KJQ qJ8zA9tIs3iMTjKPvGJySFD6iOuO7EUDvfy9dHEPDIzxMX/3HIO+2DAxc2KcA6V7Bvc+ +YLg== 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=w8uSapcKgfMjO1Om8tjS5wxWIbfF15rTJMhijIOGqBw=; b=kv+B9xMsfICF0a8lWZNRcb8+957p3tAHzLtM365kZfGgUQfLPC6odNdY+EohpSHHxs MwlgpATbWdXL3ceVTiRrHTURTDx524wJYkj5CT/Xd7TdUbCYWSWBP0hbAM4UVpAUyltI iQPbdJMFEzJ2bTd/toBgUf8LpdNo/Y6mdXkIEIldci9QsVHd0/1U3Uh0aGYF1uakND4u spnENqd3lJ53nvjWKyu+sV0eFK4IA2tVizSslepWqAjL2K+k3mYiFsnoD5OWkAXcoTbJ 8xJy1An158IDlzN/GO65o1PUpJvWKknieOu7HSTl5BTtggJXkU1FxuBCLoKlZ6A3gG7k qhGg== X-Gm-Message-State: AOAM531jwtc3ccv9eIDAgcE/ajrp3DJg67/UVUC02dIm4iqrA4Yxgt+H G3FLcDte3IiSnGQIkifKQKES1P9+IAea0m6B3OKZZoO8qA== X-Google-Smtp-Source: ABdhPJyPLvn72d3lwABAM7UK9NcbXN+l0UKyexv8K7TMfcz/Va3SItdDlQJgpuzdLlxsLrbMgmvmLp3GgkitO9It/PA= X-Received: by 2002:a5b:247:0:b0:624:4d24:94ee with SMTP id g7-20020a5b0247000000b006244d2494eemr1859544ybp.197.1650083842219; Fri, 15 Apr 2022 21:37:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jeff Janes Date: Sat, 16 Apr 2022 00:37:10 -0400 Message-ID: Subject: Re: postgresql-14 slow query To: Kenny Bachman Cc: Pgsql-admin Content-Type: multipart/alternative; boundary="00000000000043e39305dcbe15f8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000043e39305dcbe15f8 Content-Type: text/plain; charset="UTF-8" On Fri, Apr 15, 2022 at 4:59 PM Kenny Bachman wrote: > Hello Team, > > How can I tune this query? It got even slower when I created the index for > (state_id, order_id desc). The following explain analyze output is > without an index. It takes 13 seconds if I create that index. Could you > help me? > > Thank you so much for your help. > > SELECT DISTINCT ON (order_history.order_id) order_id, > order_history.creation_date AS c_date > FROM work.order_history WHERE (order_history.state_id = ANY (ARRAY[30, 51, > 63, 136, 195, 233, 348])) AND order_history.is_false = 0 > ORDER BY order_history.order_id DESC; > This query doesn't make much sense to me. You are selecting an arbitrary creation_date for each order_id, which seems like a weird thing to do on purpose. Is your ORDER BY supposed to list another column in it to break the ties? I wonder how much benefit you are actually getting from the parallel workers. If you lower max_parallel_workers_per_gather, does the plan take proportionally longer? You should turn on track_io_timing, then repeat the query with EXPLAIN (ANALYZE, BUFFERS). Cheers, Jeff --00000000000043e39305dcbe15f8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Apr 15, 2022 at 4:59 PM Kenny Bac= hman <kenny.bachman17@gmail= .com> wrote:
Hello Team,

How can I tune this query? It got even slower when I created the inde= x for (state_id, order_id desc). The following explain analyze output is wi= thout=C2=A0an=C2=A0index. It takes 13 seconds if I create that index. Could= you help me?

Thank you so much for your help.

SELECT DISTINCT ON (order_history.order_id) order_id,= order_history.creation_date =C2=A0AS c_date
FROM work.order_history WHE= RE (order_history.state_id =3D ANY (ARRAY[30, 51, 63, 136, 195, 233, 348]))= AND order_history.is_false =3D 0
ORDER BY order_history.order_id DESC;<= br>

This query doesn't make= much sense to me.=C2=A0 You are selecting an arbitrary creation_date for e= ach order_id, which seems like a weird thing to do on purpose.=C2=A0 Is you= r ORDER BY supposed to list another column in it to break the ties?

I wonder how much benefit you are actually getting from t= he parallel workers.=C2=A0 If you lower max_parallel_workers_per_gather, do= es the plan take proportionally longer?

You should= turn on track_io_timing, then repeat the query with EXPLAIN (ANALYZE, BUFF= ERS).

Cheers,

Jeff
<= /div>
--00000000000043e39305dcbe15f8--