Re: Poor performance using CTE - Mailing list pgsql-performance

From David Greco
Subject Re: Poor performance using CTE
Date
Msg-id 187F6C10D2931A4386EE8E58E13857F61291D872@BY2PRD0811MB415.namprd08.prod.outlook.com
Whole thread Raw
In response to Re: Poor performance using CTE  (Andrew Dunstan <andrew@dunslane.net>)
Responses Re: Poor performance using CTE  (Andrew Dunstan <andrew@dunslane.net>)
List pgsql-performance
You're right. I was translating an oracle query , but looks like PG will allow some syntax that is different. Trying to
findentries in fedexinvoices where smp_pkg.get_invoice_charges(id) returns a record containing charge_name in ('ADDRESS
CORRECTIONCHARGE','ADDRESS CORRECTION'). Should return the fedexinvoices row and the row from
smp_pkg.get_invoice_chargesthat contains the address correction. 


Something like this, though this is syntactically incorrect as smp_pkg.get_invoice_charges returns a set:


select fedexinvoices.*, (smp_pkg.get_invoice_charges(id)).*
from fedexinvoices
WHERE
trim(fedexinvoices.trackno)='799159791643'
and
(smp_pkg.get_invoice_charges(id)).charge_name IN ('ADDRESS CORRECTION CHARGE','ADDRESS CORRECTION')




-----Original Message-----
From: Andrew Dunstan [mailto:andrew@dunslane.net]
Sent: Wednesday, November 14, 2012 10:51 AM
To: David Greco
Cc: pgsql-performance@postgresql.org
Subject: Re: [PERFORM] Poor performance using CTE


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
anextract 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




pgsql-performance by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: Poor performance using CTE
Next
From: Andrew Dunstan
Date:
Subject: Re: Poor performance using CTE