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 1f4S3F-0005Rk-EB for pgsql-general@arkaria.postgresql.org; Fri, 06 Apr 2018 14:09:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f4S3E-0003eP-8b for pgsql-general@arkaria.postgresql.org; Fri, 06 Apr 2018 14:09:12 +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.89) (envelope-from ) id 1f4S3E-0003eF-0w for pgsql-general@lists.postgresql.org; Fri, 06 Apr 2018 14:09:12 +0000 Received: from mail-qk0-x243.google.com ([2607:f8b0:400d:c09::243]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f4S37-0006nZ-Da for pgsql-general@postgresql.org; Fri, 06 Apr 2018 14:09:11 +0000 Received: by mail-qk0-x243.google.com with SMTP id v2so1263105qkh.10 for ; Fri, 06 Apr 2018 07:09:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=Fe8Ep9OKS+Ay80B36E7wvH9qyhPTuovZNlVfy7TreRA=; b=rO2cHoGX4QmWi0g2yjuOq+i+4OrSkObEC+qcaqaFRbVxHu/0NROhQZ83A6oq+CeYGl a92+u6056RGBkVjB0oDUMkB9eR0NjIcuxcCeGSaV8flvPKwyjOD5N144O8wjuN13Gld0 WWDtWWdT4m51wyEyAYZBOd+IGNJFwHZSJj4nRzIglLY7Ysr+gZAdwDOLUH40rmNM7LUK 7V7Vr7DhxdxfmzziSmoW2Lyauf7VIGWv1ZNt1ltmMLmu1HcI4Wqf0RQ0C7dC8HbEoISn D11GcmxZH7JUumc7960zhE7asa4gC+1LQoIGA7uYGw23SDCH+34pYw94SFpwuwqZp2om 8MWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=Fe8Ep9OKS+Ay80B36E7wvH9qyhPTuovZNlVfy7TreRA=; b=lh265Cdm0bk8CjQXH1ji6W+7ETr5QMgvRVa/HrLv2fZwnSMI7LBydAaZm5MBd0qiBx 6YjE3NxSdAf9liRm0C03u8DJaJXBmXfdjtaDuxyXk156RVq17m7ZVO5FCVwc1AU5IvBE DScLGph9U2WGlNsAF7jiwOi9wAg99EnZ5yLbSY0bXx9LMLldvaWiVfvCqMCkXq7cXMAh abZddJA24HAvbNJVTf1lpRCy9+XumoCcYWXfU4bx5fY/bfSsDf+ZYitNOVXhAlYFMs+u cxfrw631GDgClWXUVKxPGVG/vvFHcrwcg/wmPnWcVAMWOQcdunpgO3OShAIT1ZlKWmxu peEg== X-Gm-Message-State: ALQs6tBMsxAmcvRlC46L1wkY5x73q7f6u+ZFnzK0CtQ84dv2q5twF6pd lO4qWaUT28Y7imYnK/Uwuhs= X-Google-Smtp-Source: AIpwx49IB8BaCZjRzcYVd7EyJe4MsDAAoR+ipb3ZhiVxTmlyi8buURScZVVy0aJpWadk03kY1U3rHw== X-Received: by 10.55.41.26 with SMTP id p26mr35979297qkh.358.1523023744070; Fri, 06 Apr 2018 07:09:04 -0700 (PDT) Received: from [192.168.245.27] ([65.183.149.206]) by smtp.gmail.com with ESMTPSA id c20sm7407722qke.38.2018.04.06.07.09.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Apr 2018 07:09:03 -0700 (PDT) Date: Fri, 6 Apr 2018 10:09:03 -0400 (EDT) From: Steven Hirsch X-X-Sender: hirsch@z87 To: pinker cc: pgsql-general@postgresql.org Subject: Re: decompose big queries In-Reply-To: <1523021756830-0.post@n3.nabble.com> Message-ID: References: <40cd1824-1dd0-1aad-2c8d-c34ee510b5c5@simkorp.com.br> <1523021756830-0.post@n3.nabble.com> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 6 Apr 2018, pinker wrote: > Edson Carlos Ericksson Richter wrote >> I don't know if there are best practices (each scenario requires its own >> solution), but for plain complex SELECT queries, I do use "WITH" >> queries... They work really well. > Be cautious with CTE's. They weren't meant to be an alternative to > subqueries and will probably change the way your query is executed, > because they are optimisation fences: > https://blog.2ndquadrant.com/postgresql-ctes-are-optimization-fences/ +1 Recently I had a poorly performing view speed up by a factor of 6x when converted from CTEs to to nested subqueries. In my case, the lack of predicate push-down was a real killer. Pg would labor away to produce an enormous intermediate result that was then selected down to a rather modest final result set. Showed up clearly in the access plan, however, and wasn't much of a problem to fix. --