Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gn5BA-0001PZ-U3 for pgsql-admin@arkaria.postgresql.org; Fri, 25 Jan 2019 17:22:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gn5B9-0000Ga-AG for pgsql-admin@arkaria.postgresql.org; Fri, 25 Jan 2019 17:22:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gn5B9-0000GS-0E for pgsql-admin@lists.postgresql.org; Fri, 25 Jan 2019 17:22:07 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gn5B6-0007Oe-II for pgsql-admin@postgresql.org; Fri, 25 Jan 2019 17:22:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x0PHM2VO032212; Fri, 25 Jan 2019 12:22:02 -0500 From: Tom Lane To: Alessandro Manzoni cc: pgsql-admin@postgresql.org Subject: Re: Strange performance problem In-reply-to: <0f9efbe6-9adc-2732-1257-b4fc0367b0f3@gmail.com> References: <0f9efbe6-9adc-2732-1257-b4fc0367b0f3@gmail.com> Comments: In-reply-to Alessandro Manzoni message dated "Fri, 25 Jan 2019 18:02:24 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <32210.1548436922.1@sss.pgh.pa.us> Date: Fri, 25 Jan 2019 12:22:02 -0500 Message-ID: <32211.1548436922@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alessandro Manzoni writes: > Then I have a complex query that has two complete different > performances, in these two cases: > case a) > with getfieldone as (select * from tableone where fieldone = 'one') > select * from getfieldone > inner join tabletwo on ... [and other joins] > case b) > select * from viewone > inner join tabletwo on ... [and other joins, the same as case a)] > I expected the same performance, but case a) lasts a few hundreds ms, > while case b) lasts more than 12 seconds. WITH is an optimization fence (at present --- there is discussion of relaxing that) so it's not exactly surprising that these queries perform differently. I am a little surprised though that case a) is the faster one; usually, preventing the planner from optimizing the whole query as one problem makes things worse. Saying more than that would require a lot more detail. https://wiki.postgresql.org/wiki/Slow_Query_Questions regards, tom lane