Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TYfFD-0003DZ-5w for pgsql-performance@postgresql.org; Wed, 14 Nov 2012 15:51:15 +0000 Received: from relay3-d.mail.gandi.net ([217.70.183.195]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TYfFB-0001lC-OC for pgsql-performance@postgresql.org; Wed, 14 Nov 2012 15:51:14 +0000 X-Originating-IP: 217.70.178.143 Received: from mfilter15-d.gandi.net (mfilter15-d.gandi.net [217.70.178.143]) by relay3-d.mail.gandi.net (Postfix) with ESMTP id 0476DA808A; Wed, 14 Nov 2012 16:51:02 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter15-d.gandi.net Received: from relay3-d.mail.gandi.net ([217.70.183.195]) by mfilter15-d.gandi.net (mfilter15-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id RXQJCvCXLseu; Wed, 14 Nov 2012 16:51:00 +0100 (CET) X-Originating-IP: 98.27.54.176 Received: from [192.168.10.146] (cpe-098-027-054-176.nc.res.rr.com [98.27.54.176]) (Authenticated sender: adsend@dunslane.net) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 44731A8068; Wed, 14 Nov 2012 16:51:00 +0100 (CET) Message-ID: <50A3BDE2.1050201@dunslane.net> Date: Wed, 14 Nov 2012 10:50:58 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: David Greco CC: "pgsql-performance@postgresql.org" Subject: Re: Poor performance using CTE References: <187F6C10D2931A4386EE8E58E13857F61291C600@BY2PRD0811MB415.namprd08.prod.outlook.com> In-Reply-To: <187F6C10D2931A4386EE8E58E13857F61291C600@BY2PRD0811MB415.namprd08.prod.outlook.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201211/163 X-Sequence-Number: 48529 On 11/14/2012 10:23 AM, David Greco wrote: > > Have a query using a CTE that is performing very poorly. The > equivalent query against the same data in an Oracle database runs in > under 1 second, in Postgres it takes 2000 seconds. > > The smp_pkg.get_invoice_charges queries fedexinvoices for some data > and normalizes it into a SETOF some record type. It is declared to be > STABLE. Fedexinvoices consists of about 1.3M rows of medium width. > Fedexinvoices.id is the primary key on that table, and > trim(fedexinvoices.trackno) is indexed via the function trim. > > The plan for the equivalent query in Oracle is much smaller and > simpler. No sequential (or full table) scans on fedexinvoices. > > WITH charges as ( > > SELECT fi2.id, smp_pkg.get_invoice_charges(fi2.id) > charge_info from fedexinvoices fi2 > > ) > > select fedexinvoices.* from > > fedexinvoices > > inner join charges on charges.id = fedexinvoices.id AND > (charges.charge_info).charge_name IN ('ADDRESS CORRECTION > CHARGE','ADDRESS CORRECTION') > > where > > trim(fedexinvoices.trackno)='799159791643' > > ; > Can you explain what you're actually trying to do here? The query looks rather odd. Why are you joining this table (or an extract from it) to itself? In any case, you could almost certainly recast it and have it run fast by first filtering on the tracking number. cheers andrew