Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VK9s8-0000oP-1A for pgsql-performance@arkaria.postgresql.org; Thu, 12 Sep 2013 16:36:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VK9s7-000725-EF for pgsql-performance@arkaria.postgresql.org; Thu, 12 Sep 2013 16:35:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VK9s5-00070E-L5 for pgsql-performance@postgresql.org; Thu, 12 Sep 2013 16:35:57 +0000 Received: from 99-153-64-76.uvs.austtx.sbcglobal.net ([99.153.64.76] helo=noel.decibel.org) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VK9rs-0003O3-CS for pgsql-performance@postgresql.org; Thu, 12 Sep 2013 16:35:57 +0000 Received: from decina.cashnetusa.com (chi-pat.cashnetusa.com [38.98.177.132]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by noel.decibel.org (Postfix) with ESMTPSA id A09376D48A; Thu, 12 Sep 2013 16:35:40 +0000 (UTC) Message-ID: <5231D950.6050502@nasby.net> Date: Thu, 12 Sep 2013 10:10:08 -0500 From: Jim Nasby User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: Bastiaan Olij CC: pgsql-performance@postgresql.org Subject: Re: Optimising views References: <522001D7.6080500@basenlily.me> In-Reply-To: <522001D7.6080500@basenlily.me> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.9 (/) 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 8/29/13 9:22 PM, Bastiaan Olij wrote: > Work well enough.. But as I'm using the same data in different reports > and I though a view might be smart. So I created a view: > ---- > create view v_costs as > select dcjobid as costjobid, sum(dcamount) as costamount > from directcosts > group by dcjobid > union all > select invjobid as costjobid, sum(detamount) as costamount from > invoiceheader > join finvoicedetail on detinvid = invid > group by invjobid > ---- > > And rewrote my report to: > ---- > select jobid, jobdesc, sum(costamount) > from jobs > join v_costs on costjobid = jobid > where > group by jobid, jobdesc > ---- > > Now what I was hoping for was that postgres would start at my jobs > table, find the records I'm trying to report on and then index scan on > the related tables and start aggregating the amounts. > What it seems to do is to first execute the view to get totals for all > the jobs in the database and join that result set with my 2 or 3 jobs > that match my filter. > > What is it about my view that prevents postgres to effectively use it? > The group bys? the union? It's probably either the GROUP BY or the UNION. Try stripping those out one at a time and see if it helps. If it doesn't, please post EXPLAIN ANALYZE (or at least EXPLAIN) output. -- Jim C. Nasby, Data Architect jim@nasby.net 512.569.9461 (cell) http://jim.nasby.net -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance