Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e8nUk-0006QQ-4Q for pgsql-performance@arkaria.postgresql.org; Sun, 29 Oct 2017 13:19:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e8nUj-0001FQ-BQ for pgsql-performance@arkaria.postgresql.org; Sun, 29 Oct 2017 13:19:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e8nSy-0006ah-NK for pgsql-performance@postgresql.org; Sun, 29 Oct 2017 13:17:28 +0000 Received: from mx.zeyos.com ([88.99.153.70]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e8nSv-0001ZG-Iw for pgsql-performance@postgresql.org; Sun, 29 Oct 2017 13:17:27 +0000 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id 45D535FB05 for ; Sun, 29 Oct 2017 14:17:21 +0100 (CET) Authentication-Results: mx.zeyos.com (amavisd-new); dkim=pass reason="pass (just generated, assumed good)" header.d=zeyos.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=zeyos.com; h= content-transfer-encoding:content-type:content-type:mime-version :to:subject:subject:from:from:date:date; s=dkim; t=1509283041; x=1510147042; bh=Sms4Fy1TM9Ef6m+wWVgCZIhpuN2ipOVYo18nl92Zzq4=; b= A5xR+UmKd6fKZOvghF1RodSKNwN1urUnpSdrcKVZkFpJnCUf7fDGUNTNJGfQdDTj Ok0jHh8mQXXZaG71Yhzk8TGGYO5haOE/cpUQtSQit6gcNtVgJJCwb5moX7iSNRwq BH8/FVqNOQ692itQCzr+cHIFN2kt1kexkM/nYCt+buE= X-Virus-Scanned: Debian amavisd-new at mx.zeyos.com Received: from mx.zeyos.com ([127.0.0.1]) by mx.zeyos.com (mx.zeyos.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id dipwRcLIA3e6 for ; Sun, 29 Oct 2017 14:17:21 +0100 (CET) Received: from [81.171.8.203] (unknown [81.171.8.203]) by mx.zeyos.com (Postfix) with ESMTPSA id C37E05FAFE; Sun, 29 Oct 2017 14:17:20 +0100 (CET) Date: Sun, 29 Oct 2017 14:17:19 +0100 From: Benjamin Coutu Subject: Re: Cheaper subquery scan not considered unless offset 0 To: David Rowley Cc: postgres performance list , Tom Lane MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20171029131721.45D535FB05@mx.zeyos.com> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org It's not a modified postgres version. It's simply for my convenience that m= y tooling calculats "total" as "actual time" multiplied by "loops". Looks l= ike I didn't properly strip that away when copy-pasting. Here are the queries and original plans again, sorry for the confusion. Query A: SELECT * FROM items a INNER JOIN ( SELECT item, sum(amount) stock FROM stocktransactions b GROUP BY item HAV= ING sum(amount) >=3D 1 ) c ON c.item =3D a."ID" Query B: SELECT * FROM items a INNER JOIN ( SELECT item, sum(amount) stock FROM stocktransactions b GROUP BY item ) c ON c.item =3D a."ID" WHERE c.stock >=3D 1 Query C: SELECT * FROM items a INNER JOIN ( SELECT item, sum(amount) stock FROM stocktransactions b GROUP BY item OFF= SET 0 ) c ON c.item =3D a."ID" WHERE c.stock >=3D 1 Queries A + B generate the same plan and execute as follows: Merge Join (cost=3D34935.30..51701.59 rows=3D22285 width=3D344) (actual ti= me=3D463.824..659.553 rows=3D15521 loops=3D1) Merge Cond: (a."ID" =3D b.item) -> Index Scan using "PK_items_ID" on items a (cost=3D0.42..15592.23 row= s=3D336083 width=3D332) (actual time=3D0.012..153.899 rows=3D336064 loops= =3D1) -> Sort (cost=3D34934.87..34990.59 rows=3D22285 width=3D12) (actual tim= e=3D463.677..466.146 rows=3D15521 loops=3D1) Sort Key: b.item Sort Method: quicksort Memory: 1112kB -> Finalize HashAggregate (cost=3D32879.78..33102.62 rows=3D22285= width=3D12) (actual time=3D450.724..458.667 rows=3D15521 loops=3D1) Group Key: b.item Filter: (sum(b.amount) >=3D '1'::double precision) Rows Removed by Filter: 48277 -> Gather (cost=3D27865.65..32545.50 rows=3D44570 width=3D1= 2) (actual time=3D343.715..407.243 rows=3D162152 loops=3D1) Workers Planned: 2 Workers Launched: 2 -> Partial HashAggregate (cost=3D26865.65..27088.50 r= ows=3D22285 width=3D12) (actual time=3D336.416..348.105 rows=3D54051 loops= =3D3) Group Key: b.item -> Parallel Seq Scan on stocktransactions b (co= st=3D0.00..23281.60 rows=3D716810 width=3D12) (actual time=3D0.015..170.646= rows=3D579563 loops=3D3) Planning time: 0.277 ms Execution time: 661.342 ms Plan C though, thanks to the "offset optimization fence", executes the foll= owing, more efficient plan: Nested Loop (cost=3D32768.77..41146.56 rows=3D7428 width=3D344) (actual ti= me=3D456.611..525.395 rows=3D15521 loops=3D1) -> Subquery Scan on c (cost=3D32768.35..33269.76 rows=3D7428 width=3D12= ) (actual time=3D456.591..475.204 rows=3D15521 loops=3D1) Filter: (c.stock >=3D '1'::double precision) Rows Removed by Filter: 48277 -> Finalize HashAggregate (cost=3D32768.35..32991.20 rows=3D22285= width=3D12) (actual time=3D456.582..468.124 rows=3D63798 loops=3D1) Group Key: b.item -> Gather (cost=3D27865.65..32545.50 rows=3D44570 width=3D1= 2) (actual time=3D348.479..415.463 rows=3D162085 loops=3D1) Workers Planned: 2 Workers Launched: 2 -> Partial HashAggregate (cost=3D26865.65..27088.50 r= ows=3D22285 width=3D12) (actual time=3D343.952..355.912 rows=3D54028 loops= =3D3) Group Key: b.item -> Parallel Seq Scan on stocktransactions b (co= st=3D0.00..23281.60 rows=3D716810 width=3D12) (actual time=3D0.015..172.235= rows=3D579563 loops=3D3) -> Index Scan using "PK_items_ID" on items a (cost=3D0.42..1.05 rows=3D= 1 width=3D332) (actual time=3D0.003..0.003 rows=3D1 loops=3D15521) Index Cond: ("ID" =3D c.item) Planning time: 0.223 ms Execution time: 526.203 ms =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Original =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D From: David Rowley To: Benjamin Coutu Date: Sun, 29 Oct 2017 12:46:42 +0100 Subject: Re: [PERFORM] Cheaper subquery scan not considered unless offset 0 >=20 >=20 > On 30 October 2017 at 00:24, Benjamin Coutu wrote: > > -> Index Scan using "PK_items_ID" on items a (cost=3D0.42..1.05 row= s=3D1 width=3D332) (actual time=3D0.003..0.003 rows=3D1 loops=3D15521 total= =3D46.563) >=20 > I've never seen EXPLAIN output like that before. >=20 > Is this some modified version of PostgreSQL? > --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance