X-Original-To: pgsql-novice-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 69A083AC4D6; Wed, 15 Dec 2004 18:00:14 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44834-04; Wed, 15 Dec 2004 18:00:03 +0000 (GMT) Received: from pillette.com (adsl-67-119-5-202.dsl.snfc21.pacbell.net [67.119.5.202]) by svr1.postgresql.org (Postfix) with ESMTP id 3C74F3B4BEC; Wed, 15 Dec 2004 17:59:59 +0000 (GMT) Received: from [192.168.1.200] (dhcp200.pillette.com [192.168.1.200]) by pillette.com (8.11.6/8.11.6) with ESMTP id iBFHxtx18988; Wed, 15 Dec 2004 09:59:55 -0800 Message-ID: <41C07CFD.4020703@pillette.com> Date: Wed, 15 Dec 2004 10:05:49 -0800 From: Andrew Lazarus Reply-To: andrew@pillette.com Organization: Pillette Investment Management User-Agent: Mozilla Thunderbird 0.8 (Windows/20040913) X-Accept-Language: en-us, en MIME-Version: 1.0 To: sarlav kumar Cc: pgsqlperform , pgsql-novice@postgresql.org Subject: Re: [PERFORM] Query Optimization References: <20041214213407.84233.qmail@web51307.mail.yahoo.com> In-Reply-To: <20041214213407.84233.qmail@web51307.mail.yahoo.com> Content-Type: multipart/mixed; boundary="------------060800090608030806010903" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200412/152 X-Sequence-Number: 11729 This is a multi-part message in MIME format. --------------060800090608030806010903 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 ||'
'||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. --------------060800090608030806010903 Content-Type: text/x-vcard; charset=utf-8; name="andrew.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="andrew.vcf" begin:vcard fn:Andrew Lazarus n:Lazarus;Andrew org:Pillette Investment Management;Research and Development adr;dom:;;3028 Fillmore;San Francisco;CA;94123 email;internet:andrew@pillette.com title:Director tel;work:800-366-0688 tel;fax:415-440-4093 url:http://www.pillette.com version:2.1 end:vcard --------------060800090608030806010903--