Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duiAx-0004WZ-5N for pgsql-performance@arkaria.postgresql.org; Wed, 20 Sep 2017 16:48:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1duiAw-0007xn-OG for pgsql-performance@arkaria.postgresql.org; Wed, 20 Sep 2017 16:48:38 +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 1dui99-00044C-UI for pgsql-performance@postgresql.org; Wed, 20 Sep 2017 16:46:48 +0000 Received: from mail-qt0-x22b.google.com ([2607:f8b0:400d:c0d::22b]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dui97-00013U-5B for pgsql-performance@postgresql.org; Wed, 20 Sep 2017 16:46:46 +0000 Received: by mail-qt0-x22b.google.com with SMTP id o3so2364065qte.6 for ; Wed, 20 Sep 2017 09:46:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Dp7DB/GfbIpRhzIBJOilBhJ5sKg8CftCIQfXRLFB0/o=; b=Mz2+4z8Nl6Xqll0zgZwLCBZAHVwIZeRJ/UNaQ/Jy45GER2QwipuVyXT+jK1n19rlFZ iwbFpWNn1oN8R0pE0cZ8WctQZAmMbBGOo3h0r2sqL92H8hgh0T15P8x7/ZXb/jlt/DpT 4WLlHn0WwOIdOlaOg2B4hgzEyIcMXM4pr95OGNSXNOKqB5Zw1Qms+V48a5lppnVfGOeD FUTOJ6QXuhHKQ4pa0eUHebIrTmY1cLvd+MF7MOmb3nI67TwVIVxCWrvV9C97zxtKPK3I zEarEWFQJmu825NdyglNtcEZngu05KPhGQg/V1r6Btlr32RoH7BW74VY12WTCVef8L7B 3ePw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Dp7DB/GfbIpRhzIBJOilBhJ5sKg8CftCIQfXRLFB0/o=; b=ebMgVmIg/c73BiD51379lkjHvgVm+9GtOZq65CobgbvHSpmIy7FAcWuw9qiLkE2BKU 0uTwm5DjiBu/EhSqxtVZcL4vCIpYnIyy5RlcW4NUe5r3eAB3Pc+32wzf8buzzsF9zDyo ll8rL5Qf5Hes62EpGUVMYvlI1eAhmtB36vw8ZjNgIi1kfWIii7m+GAjw2D6EMJuJ/02a oDR7ZXAiXlJIFvtmj81ryrnwGHNWNJiuGNmeMTuovEFA9X+GE06+y25Tu1eDc0XmoZmJ 8L4zT47KTOCKpHC9C9mFj34lJmRwTMekP4TSgg1zVl2uq9ygxA50sprDL6I31FRNZ5YJ Q7DQ== X-Gm-Message-State: AHPjjUj14vdkOQfROe2ErCvxMkRmgJooXcs+SLDDq4EmNuEPraefVVp4 I6HCSyFAC8Phxnc6q55xy5J1R3DHo0R6qz5u3jzqz6I= X-Google-Smtp-Source: AOwi7QAqqTN34CgjOP+nU9OiV5P0AAUVwjhRfsQIV4G3QJjlgsY70oximhrOrsWiNSqlNPq1myGupKyszuDDY1P9NBE= X-Received: by 10.200.9.61 with SMTP id t58mr8595445qth.337.1505926003865; Wed, 20 Sep 2017 09:46:43 -0700 (PDT) MIME-Version: 1.0 Received: by 10.140.84.35 with HTTP; Wed, 20 Sep 2017 09:46:43 -0700 (PDT) In-Reply-To: References: From: Jeff Janes Date: Wed, 20 Sep 2017 09:46:43 -0700 Message-ID: Subject: Re: repeated subplan execution To: monika yadav Cc: "pgsql-performance@postgresql.org" Content-Type: multipart/alternative; boundary="001a1144e25a84bef00559a1b815" 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 --001a1144e25a84bef00559a1b815 Content-Type: text/plain; charset="UTF-8" On Tue, Sep 19, 2017 at 7:31 PM, monika yadav wrote: > Hi All, > > I didn't understand why same sub plan for the sub query executed two > times? As per the query it should have been executed only once. > > Can someone please explain this behaviour of query execution ? > The sum_bid at the end of the query is an alias for the entire subselect, so it not entirely surprising that it gets interpolated twice. it is just kind of unfortunate from a performance perspective. The query I originally gave is equivalent to this query: select aid, (select sum(bid) from pgbench_branches where bbalance between -10000-abalance and 1+abalance ) as sum_bid from pgbench_accounts where aid between 1 and 1000 group by aid having (select sum(bid) from pgbench_branches where bbalance between -10000-abalance and 1+abalance ) >0; In my originally query I just wrapped the whole thing in another select, so that I could use the alias rather than having to mechanically repeat the entire subquery again in the HAVING section. They give identical plans. Cheers, Jeff --001a1144e25a84bef00559a1b815 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On T= ue, Sep 19, 2017 at 7:31 PM, monika yadav <monika.1234yadav@gmail= .com> wrote:
Hi All,

I didn't understand why same sub plan for the sub query exe= cuted two times? As per the query it should have been executed only once.

Can someone please explai= n this behaviour of query execution ?=C2=A0

The sum_bid at the end of the query is an alias= for the entire subselect, so it not entirely surprising that it gets inter= polated twice. it is just kind of unfortunate from a performance perspectiv= e.

The query I originally gave is equivalent to th= is query:


=C2=A0select
=C2=A0 =C2=A0 =C2=A0 =C2=A0 aid,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (= select sum(bid) from pgbench_branches
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 where bbalance between -10000-abalance and 1+abalance
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 ) as sum_bid
=C2=A0 =C2=A0 =C2=A0= =C2=A0 from pgbench_accounts
=C2=A0 =C2=A0 =C2=A0 =C2=A0 where a= id between 1 and 1000
=C2=A0 =C2=A0 =C2=A0 =C2=A0 group by aid=C2= =A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 having (select sum(bid) from pgbe= nch_branches where bbalance between -10000-abalance and 1+abalance ) >0;=


In my originally query I jus= t wrapped the whole thing in another select, so that I could use the alias = rather than having to mechanically repeat the entire subquery again in the = HAVING section.=C2=A0 They give identical plans.

C= heers,

Jeff
--001a1144e25a84bef00559a1b815--