Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YXZgt-0002jg-79 for pgsql-performance@arkaria.postgresql.org; Mon, 16 Mar 2015 18:24:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YXZgs-0006Dn-7q for pgsql-performance@arkaria.postgresql.org; Mon, 16 Mar 2015 18:24:38 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YXZgq-0006DF-Qx for pgsql-performance@postgresql.org; Mon, 16 Mar 2015 18:24:36 +0000 Received: from mail-we0-f181.google.com ([74.125.82.181]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YXZgn-0002sJ-Ac for pgsql-performance@postgresql.org; Mon, 16 Mar 2015 18:24:34 +0000 Received: by weop45 with SMTP id p45so19226207weo.0 for ; Mon, 16 Mar 2015 11:24:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:organization:user-agent :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=BFuhMxE1Skjw8sv0w6BLhvvzd46IYkoH12m/PIctxFI=; b=LdCGYTlgtsynJjzuuC84AQLh4gJj1aOVCMSsHqLcgZR/crqY1H1LSmDUEfip2oNIJT C21QxwzONkPaBH5e5MHAF9Wj4gd8TXroP+vWOt9QLukaw2wVgFtG7IXPD2qR1qspu8Tv qQKJS7yTPjT4cxWSABZU4jPeGfVe1G8Gq/V7xZWBn1/Zf/NAIbP5fPNB3OxKESlVLRsz OvOvL9T2E1WMSXACyqtuViVO2O+pfjKikvXz6kuBcLU7GPPQWXQTfDDi89NvkhkLaHfe SQWHHA0QV3dPEUx54Tf4i6Ec7jHax0aYiwwPxpt4RXsXgiYh9pcqI1k4zVj2TfVblbfd T5jA== X-Gm-Message-State: ALoCoQnYSGU73m2ook6EMPg3ZNZ4P4EUyO8/7NSUdICWmn/ieR85Cp2zTSMAJJatjom+moMklw8QLzeWn7YjmPWwNqPnzEtDMg2bTGzRvJq7yYcoPKWvjA0Pb3xoKFr5AS9vdQC4iT53szM85xKqe0ZPLi4jeREniI3qqThux4Ws0kAfLIp1YcKk0JUkmnhQCgtlKuU59W3c X-Received: by 10.180.78.136 with SMTP id b8mr126687631wix.6.1426530271181; Mon, 16 Mar 2015 11:24:31 -0700 (PDT) Received: from [192.168.1.163] (ip-78-45-139-138.net.upcbroadband.cz. [78.45.139.138]) by mx.google.com with ESMTPSA id hd10sm16447044wib.7.2015.03.16.11.24.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Mar 2015 11:24:30 -0700 (PDT) Message-ID: <55071FD8.6050106@2ndquadrant.com> Date: Mon, 16 Mar 2015 19:24:24 +0100 From: Tomas Vondra Organization: 2ndQuadrant User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Performance issues References: <84a6fe4be04c3ef0a714851977860ac8@mail.gmail.com> <160b8780d89add7f23c4eb9c3debb99d@mail.gmail.com> <55034A48.4040409@2ndquadrant.com> <849e75d9463938372dd4c516f5c5d89c@mail.gmail.com> <55037CEA.5050906@2ndquadrant.com> <5504A2CE.30007@archidevsys.co.nz> <5504A8E7.7080008@archidevsys.co.nz>, <698495ca8f09d9b320b81b381d5e7540@mail.gmail.com> In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) 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 On 16.3.2015 18:49, Marc Mamin wrote: > >> Hi Team, >> >> This is the EXPLAIN ANALYZE for one of the view : S_V_D_CAMPAIGN_HIERARCHY: FWIW, this is a somewhat more readable version of the plan: http://explain.depesz.com/s/nbB In the future, please do two things: (1) Attach the plan as a text file, because the mail clients tend to screw things up (wrapping long lines). Unless the plan is trivial, of course - but pgsql-performance usually deals with complex stuff. (2) Put the plan on explain.depesz.com helps too, because it's considerably more readable (but always do 1, because resorces placed somewhere else tends to disappear, and the posts then make very little sense, which is bad when searching in the archives) (3) Same for stuff pasted somewhere else - always attach it to the message. For example I'd like to give you more accurate advice, but I can't as http://pgsql.privatepaste.com/41207bea45 is unavailable. > > > >Rows Removed by Join Filter: 3577676116 > > That's quite a lot. > You're possibly missing a clause in a join, resulting in a cross join. > It is also helpful to put your result here: > http://explain.depesz.com/ > regards, IMHO this is merely a consequence of using the CTE, which produces 52997 rows and is scanned 67508x as the inner relation of a nested loop. That gives you 3577721476 tuples in total, and only 45360 are kept (hence 3577676116 are removed). This is a prime example of why CTEs are not just aliases for subqueries, but may actually cause serious trouble. There are other issues (e.g. the row count estimate of the CTE is seriously off, most likely because of the HashAggregate in the outer branch), but that's a secondary issue IMHO. Vivekanand, try this (in the order of intrusiveness): (1) Get rid of the CTE, and just replace it with subselect in the FROM part of the query, so instead of this: WITH valid_executions AS (...) SELECT ... FROM ... JOIN valid_executions ON (...) you'll have something like this: SELECT ... FROM ... JOIN (...) AS valid_executions ON (...) This way the subselect will optimized properly. (2) Replace the CTE with a materialized view, or a temporary table. This has both advantages and disadvantages - the main advantage is that you can create indexes, collect statistics. Disadvantage is you have to refresh the MV, fill temporary table etc. I expect (1) to improve the performance significantly, and (2) might improve it even further by fixing the misestimates. regards -- Tomas Vondra http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance