Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fr29g-0000iL-J1 for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 14:24:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fr29e-0003Jy-Mp for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 14:24: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.89) (envelope-from ) id 1fr29d-0003Jr-1g for pgsql-general@lists.postgresql.org; Sat, 18 Aug 2018 14:24:38 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fr29Z-0005wU-1l for pgsql-general@postgresql.org; Sat, 18 Aug 2018 14:24:35 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 07FF4262; Sat, 18 Aug 2018 10:24:30 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 18 Aug 2018 10:24:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=5eekc/WQ8hsDD3dKwS2wc0nZF6v9H 0lgqe0E6xXUMi8=; b=j2LWMa+5+L/C8bMahC3ZjEUYSqqEE6kt+EtVzMqVKxOTm 8oEnmx3xOtIxFSXQQmhtvuPfjYKaPc085gyJcpo/CPReUJ5I5YjAE3jr/ORynBo0 iA7zZIQJDsyGiUH0GiwvHHKGw6/XPVmYrv7SZ4uhfFU8X9CLIElCsoWJxqY+YHpN thqFHYF1Kec9qu/i8ibjABD0udGAlX+mn3Kq1HfW6dhd3W6KAozlef1twOtlHd0M 19s+q9xbJjc+cjfFwo4/OcQpJrUdWaAmpnz3Dv6SikXOPVaGzzJww24IyOhK8aL9 eZuV4iOBBua9szKw45As+81w+ykmc5xwpEsOuuCJQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=5eekc/ WQ8hsDD3dKwS2wc0nZF6v9H0lgqe0E6xXUMi8=; b=og57+Y7DVw4xZ7tzg6TRfq nrrmP7GyuXym9cZw82ywkNja1UiBUy9l6RyzttRkF9jjLrpWewiPL9Q7Be437aRO tUaJ3IhjTcANZjgSFggqFz0uk4NxoykekouPPRBB8010ixz6iusUOlo8amB3pX6b 7yweAeykCcZpez60gIg1S6PcdYkHvz5cP9oVoh/ERNz0eidPxdJjeBU0UPu0zRUG b2l+/t1jsbnIAZLBfe/R7zLGvCA/m3cTofWsjh+OFl3PJde38uERnggAZse4gQEX qwCJEkNrxmEumFgxAQO/CDuhubxX0PQNYhAxKuaucQROt/yl5AinAdrHPnIwhx8Q == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 70700E48FB; Sat, 18 Aug 2018 10:24:29 -0400 (EDT) Subject: Re: AW: CTE with JOIN of two tables is much faster than a regular query To: kpi6288@gmail.com, pgsql-general@postgresql.org References: <004b01d436d6$ff96c840$fec458c0$@gmail.com> <42ce9b74-0671-ecba-3deb-dc8bc8d5e9b3@a-kretschmer.de> <005301d436e3$cb120960$61361c20$@gmail.com> From: Adrian Klaver Message-ID: Date: Sat, 18 Aug 2018 07:24:28 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <005301d436e3$cb120960$61361c20$@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 08/18/2018 04:08 AM, kpi6288@gmail.com wrote: > > >> -----Ursprüngliche Nachricht----- >> Von: Andreas Kretschmer >> Gesendet: Samstag, 18. August 2018 12:27 > >> Am 18.08.2018 um 11:36 schrieb kpi6288@gmail.com: >>> What can I do to improve the performance of the regular query without >>> using a CTE? >> >> try to rewrite it to a subselect: >> >> select ... from ... join (selec ... from ... where ...) x on ... >> > > Do mean like this? > > EXPLAIN ANALYSE > SELECT F.oID, D.szText > FROM F > JOIN (SELECT Doc.uDocID, Doc.szText FROM Doc WHERE szText ILIKE '%480GB%') > AS D ON D.uDocID = F.uDocRef; To try to replicate what the CTE is doing I would try: SELECT * FROM Doc JOIN (SELECT uDocRef, F.oID, Doc.szText FROM F JOIN Doc ON F.uDocRef = Doc.udocid ) AS D ON D.uDocRef = Doc.udocid WHERE D.szText ILIKE '%480GB%' > > Just as bad as my regular query: > > "Hash Join (cost=8006.56..8694.93 rows=5 width=359) (actual > time=66777.898..66784.630 rows=10 loops=1)" > " Hash Cond: (f.udocref = doc.udocid)" > " -> Seq Scan on f (cost=0.00..566.05 rows=32605 width=32) (actual > time=0.002..3.563 rows=32605 loops=1)" > " -> Hash (cost=8006.32..8006.32 rows=19 width=359) (actual > time=66777.471..66777.471 rows=16 loops=1)" > " Buckets: 1024 Batches: 1 Memory Usage: 19kB" > " -> Seq Scan on doc (cost=0.00..8006.32 rows=19 width=359) (actual > time=9013.317..66777.438 rows=16 loops=1)" > " Filter: (sztext ~~* '%480GB%'::text)" > " Rows Removed by Filter: 125930" > "Planning time: 236.354 ms" > "Execution time: 66784.651 ms" > > > -- Adrian Klaver adrian.klaver@aklaver.com