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 1nfU9H-0005vx-E2 for pgsql-admin@arkaria.postgresql.org; Fri, 15 Apr 2022 22:10:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nfU9G-0002k2-Bu for pgsql-admin@arkaria.postgresql.org; Fri, 15 Apr 2022 22:10:38 +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 <010001802f472693-b1f90032-4f2d-4ba4-8554-4732c19ca814-000000@amazonses.com>) id 1nfU9F-0002jt-VW for pgsql-admin@lists.postgresql.org; Fri, 15 Apr 2022 22:10:38 +0000 Received: from a48-116.smtp-out.amazonses.com ([54.240.48.116]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.92) (envelope-from <010001802f472693-b1f90032-4f2d-4ba4-8554-4732c19ca814-000000@amazonses.com>) id 1nfU9D-0001wC-C5 for pgsql-admin@lists.postgresql.org; Fri, 15 Apr 2022 22:10:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=6gbrjpgwjskckoa6a5zn6fwqkn67xbtw; d=amazonses.com; t=1650060633; h=Content-Type:Content-Transfer-Encoding:From:Mime-Version:Subject:Date:Message-Id:References:Cc:In-Reply-To:To:Feedback-ID; bh=da9LYbAFsWK/Xusj2e/YsLU7Hd3K8a2VZq/qsGqshdY=; b=Y32DlROZQr2WDwVSrFfAc6tF8t+WamZ0U+BpqtvBBYrYagt0SHaBqfK6wvRB3rE1 UHCR7ys1rPy25zfDpnYP6XRDekNa5p65Wkf9zUrJ+aojehdiN3GfIxiZbnk03b8it0U Z2NQpklr4fbUfe42NQY/o2WnETwmmIjJjE3GKM84= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Doug Reynolds Mime-Version: 1.0 (1.0) Subject: Re: postgresql-14 slow query Date: Fri, 15 Apr 2022 22:10:33 +0000 Message-ID: <010001802f472693-b1f90032-4f2d-4ba4-8554-4732c19ca814-000000@email.amazonses.com> References: Cc: pgsql-admin@lists.postgresql.org In-Reply-To: To: Kenny Bachman X-Virus-Scanned: clamav-milter 0.103.2 at postfix X-Virus-Status: Clean X-Mailer: iPhone Mail (19E258) Feedback-ID: 1.us-east-1.L+CISTGIImABHDx8bBKKEgULZ36xzLwmtjped/xJNPU=:AmazonSES X-SES-Outgoing: 2022.04.15-54.240.48.116 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I would move the WHERE filter into a subquery and then run the ORDER BY/DIST= INCT on the subquery result. I like to avoid DISTINCT on queries using an A= NTIJOIN or SEMIJOIN if possible. It is hard to recommend without knowing yo= ur data. Sent from my iPhone > On Apr 15, 2022, at 4:59 PM, Kenny Bachman wro= te: >=20 > =EF=BB=BF > Hello Team, >=20 > 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 a= n index. It takes 13 seconds if I create that index. Could you help me? >=20 > Thank you so much for your help. >=20 > SELECT DISTINCT ON (order_history.order_id) order_id, order_history.creati= on_date AS c_date > FROM work.order_history WHERE (order_history.state_id =3D ANY (ARRAY[30, 5= 1, 63, 136, 195, 233, 348])) AND order_history.is_false =3D 0 > ORDER BY order_history.order_id DESC; >=20 > EXPLAIN ANALYZE output: >=20 > Unique (cost=3D672007.46..1519683.55 rows=3D206423 width=3D12) (actual t= ime=3D1701.420..3439.095 rows=3D2049357 loops=3D1) > -> Gather Merge (cost=3D672007.46..1502346.48 rows=3D6934827 width=3D= 12) (actual time=3D1701.419..2989.243 rows=3D6891551 loops=3D1) > Workers Planned: 4 > Workers Launched: 4 > -> Sort (cost=3D671007.40..675341.67 rows=3D1733707 width=3D12)= (actual time=3D1657.609..1799.723 rows=3D1378310 loops=3D5) > Sort Key: order_id DESC > Sort Method: external merge Disk: 38960kB > Worker 0: Sort Method: external merge Disk: 31488kB > Worker 1: Sort Method: external merge Disk: 36120kB > Worker 2: Sort Method: external merge Disk: 31368kB > Worker 3: Sort Method: external merge Disk: 36152kB > -> Parallel Seq Scan on order_history (cost=3D0.00..47399= 3.00 rows=3D1733707 width=3D12) (actual time=3D0.041..1211.485 rows=3D137831= 0 loops=3D5) > Filter: ((is_false =3D 0) AND (state_id =3D ANY ('{30= ,51,63,136,195,233,348}'::integer[]))) > Rows Removed by Filter: 3268432 > Planning Time: 0.405 ms > Execution Time: 3510.433 ms