Skip site navigation (1) Skip section navigation (2)

Re: [PERFORM] Query Optimization

From: Andrew Lazarus <andrew(at)pillette(dot)com>
To: sarlav kumar <sarlavk(at)yahoo(dot)com>
Cc: pgsqlperform <pgsql-performance(at)postgresql(dot)org>,pgsql-novice(at)postgresql(dot)org
Subject: Re: [PERFORM] Query Optimization
Date: 2004-12-15 18:05:49
Message-ID: 41C07CFD.4020703@pillette.com (view raw or flat)
Thread:
Lists: pgsql-novicepgsql-performance
sarlav kumar wrote:
> Hi all,
>  
> Can someone please help me optimize this query? Is there a better way to 
> write this query? I am generating a report of transactions ordered by 
> time and with details of the sender and receiver etc.
>  
> SELECT distinct a.time::date ||'<br>'||substring(a.time::time::text,1,8) 
> as Time,
> CASE WHEN a.what = 0 THEN 'Money Transfer' WHEN a.what = 15 THEN 
> 'Purchase' WHEN a.what = 26 THEN 'Merchant Streamline' WHEN a.what = 13 
> THEN 'Reversal' END  as Transaction_Type ,
> c1.account_no as SenderAccount, c2.account_no as RecieverAccount,
> b.country as SenderCountry, d.country as RecieverCountry,
> b.firstname as SenderFirstName, b.lastname as SenderLastName,
> d.firstname as ReceiverFirstName, d.lastname as ReceiverLastName,
> a.status as status,
> (select sum(td.amount * 0.01) from transaction_data td where td.data_id 
> = a2.id and td.dir = 1 and td.uid = a.target_uid) as ReversedAmount,
> (select sum(td.amount * 0.01) from transaction_data td where td.data_id 
> = a2.id and td.dir = 0 and td.uid = a.uid ) as DepositedAmount, a.flags, 
> (a.amount * 0.01) as Amount,
> (a.fee * 0.01) as Fee
> FROM data a, customerdata b, customerdata d, customer c1, customer c2 , 
> participant p, data a2
> WHERE p.id = a.partner_id AND (a.uid = c1.id) AND (a.target_uid = c2.id) 
> and c1.id=b.uid and c2.id=d.uid
> and a.confirmation is not null AND (a2.ref_id = a.id) and
> ((a2.what = 13) or (a2.what = 17) ) ORDER BY time desc ;
(query plan followed)

The expensive operation is the UNIQUE. Are you sure, in terms of 
business logic, that this is necessary? Is it actually possible to have 
duplicate transactions at the exact same time, and if so, would you 
really want to eliminate them?

As an aside, I prefer to have numeric constants like the 'what' field in 
a small lookup table of two columns (what_code, what_description); it's 
easier to extend and to document.

Attachment: andrew.vcf
Description: text/x-vcard (298 bytes)

In response to

pgsql-novice by date

Next:From: Ragnar HafstaĆ°Date: 2004-12-15 18:23:43
Subject: Re: [PERFORM] \d output to a file
Previous:From: Kretschmer AndreasDate: 2004-12-15 18:04:17
Subject: Re: \d output to a file

pgsql-performance by date

Next:From: Ragnar HafstaĆ°Date: 2004-12-15 18:23:43
Subject: Re: [PERFORM] \d output to a file
Previous:From: Kretschmer AndreasDate: 2004-12-15 18:04:17
Subject: Re: \d output to a file

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group