Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1egnzH-0002K4-At for pgsql-performance@arkaria.postgresql.org; Wed, 31 Jan 2018 08:43:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1egnzG-0006iy-0U for pgsql-performance@arkaria.postgresql.org; Wed, 31 Jan 2018 08:43:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1egnzF-0006in-Ow for pgsql-performance@lists.postgresql.org; Wed, 31 Jan 2018 08:43:21 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1egnz4-00014K-Lo for pgsql-performance@postgresql.org; Wed, 31 Jan 2018 08:43:21 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 79D0A42BD1; Wed, 31 Jan 2018 09:43:02 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id RDG-zt4WB9Go; Wed, 31 Jan 2018 09:43:00 +0100 (CET) Message-ID: <1517388179.2579.15.camel@cybertec.at> Subject: Re: Nested Loops From: Laurenz Albe To: "Kumar, Virendra" , "pgsql-performance@postgresql.org" Date: Wed, 31 Jan 2018 09:42:59 +0100 In-Reply-To: <91e77e2bd0494c8e9c20859e39df7553@USFKL11XG20CN01.mercer.com> References: <91e77e2bd0494c8e9c20859e39df7553@USFKL11XG20CN01.mercer.com> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Kumar, Virendra wrote: > Can somebody help me avoid nested loops in below query: > -- > ap_poc_db=# explain (analyze,buffers) > ap_poc_db-# select site_id, account_id FROM ap.site_exposure se > ap_poc_db-# WHERE se.portfolio_id=-1191836 > ap_poc_db-# AND EXISTS (select 1 from ap.catevent_flood_sc_split sp where sp.migration_sourcename= 'KatRisk_SC_Flood_2015_v9' AND ST_Intersects(se.shape, sp.shape)) > ap_poc_db-# group by site_id, account_id; > QUERY PLAN > -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...] > Buffers: shared hit=172041 > -> Gather Merge (cost=23479854.04..23479879.04 rows=205 width=16) (actual time=1387.823..1388.676 rows=1532 loops=1) > Workers Planned: 5 > Workers Launched: 5 > Buffers: shared hit=172041 [...] > -> Nested Loop Semi Join (cost=4.53..23478852.87 rows=41 width=16) (actual time=34.772..1345.489 rows=255 loops=6) > Buffers: shared hit=864235 > -> Append (cost=0.00..156424.56 rows=123645 width=48) (actual time=1.011..204.748 rows=102990 loops=6) > Buffers: shared hit=154879 > -> Parallel Seq Scan on site_exposure_1191836 se (cost=0.00..156424.56 rows=123645 width=48) (actual time=1.004..187.702 rows=102990 loops=6) > Filter: (portfolio_id = '-1191836'::integer) > Buffers: shared hit=154879 > -> Bitmap Heap Scan on catevent_flood_sc_split sp (cost=4.53..188.54 rows=15 width=492) (actual time=0.007..0.007 rows=0 loops=617937) > Recheck Cond: (se.shape && shape) > Filter: ((migration_sourcename = 'KatRisk_SC_Flood_2015_v9'::bpchar) AND _st_intersects(se.shape, shape)) > Rows Removed by Filter: 0 > Heap Blocks: exact=1060 > Buffers: shared hit=709356 > -> Bitmap Index Scan on catevent_flood_sc_split_shape_mig_src_gix (cost=0.00..4.52 rows=45 width=0) (actual time=0.005..0.005 rows=0 loops=617937) > Index Cond: (se.shape && shape) > Buffers: shared hit=691115 > Planning time: 116.141 ms > Execution time: 1391.785 ms With a join condition like that (using on a function result), only a nested loop join is possible. I don't know how selective sp.migration_sourcename= 'KatRisk_SC_Flood_2015_v9' is; perhaps an index on the column can help a little. But you won't get around the 617937 loops, which is the cause of the long query duration. I don't think there is a lot of potential for optimization. Yours, Laurenz Albe