Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 842C72E01AE for ; Mon, 28 Jan 2008 08:27:03 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 59300-06 for ; Mon, 28 Jan 2008 08:26:50 -0400 (AST) Received: from ns1.officenet.no (ns1.officenet.no [193.212.174.3]) by postgresql.org (Postfix) with ESMTP id E4B272E01BB for ; Mon, 28 Jan 2008 08:26:56 -0400 (AST) Received: from [89.10.27.2] (helo=andreak-laptop.local) by ns1.officenet.no with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1JJT4Z-00035y-Fr; Mon, 28 Jan 2008 13:26:53 +0100 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Re: Slow Query problem Date: Mon, 28 Jan 2008 13:26:33 +0100 User-Agent: KMail/1.9.6 (enterprise 0.20070907.709405) Cc: "Premsun Choltanwanich" References: <479DE418.C5F7.004C.0@nsasia.co.th> In-Reply-To: <479DE418.C5F7.004C.0@nsasia.co.th> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200801281326.33832.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/215 X-Sequence-Number: 30068 On Monday 28 January 2008 08:18:24 Premsun Choltanwanich wrote: > Dear All, > > I am currently using PostgreSQL database version 8.0.13. My problem > relates to a slow result when a query using a defined view joins to anoth= er > table for a result. > > Background: I have 7 tables of invoice transactions. The tables are > slightly different in that they record different data (some different > columns in each table). There are about 250,000 records when a union view > is created. A simply query on this union performs satisfactorily. > > The invoice table union view is then joined with a table of receipts (whi= ch > have a total of about 150,000 records). > > It takes around 3.5 seconds for "select * from view_transaction where > member_id =3D 999 and receipt_no is null" (which returns unpaid invoices= ). > > By hard coding I created a single table from the 7 invoice tables (instead > of creating a union) and then used it with receipt table. This time for > the same query improved to 1.8 seconds. > > To further improve things I tried to code the selection rather than to use > a view, and so "select * from temp_transaction where member_id =3D 999 and > receipt_no is null" provided the result in .5 second. (2 records returned > containing the details of receipt_no, transaction_no, transaction_type, > transaction_amount, member_id). > > I would prefer to be able to have completed the above by using unions and > views. Is it possible to do this, or am I better creating a permanent > table of invoices and writing the query as I did above? > > Any comments on this and suggestions would be appreciated. If there is > documentation where I can read up please let me have a link. It is very hard to help without you providing the schema for the tables/vie= ws=20 involved. It sounds like you don't have any indexes if you experience=20 performance-problems on queries like "select * from view_transaction where= =20 member_id =3D 999 and receipt_no is null". But again, without the definiti= on=20 of the view and underlying tables, it's very hard to help. =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Karenslyst All=C3=A9 11 | know how to do a thing and to watch | PO. Box 529 Sk=C3=B8yen | somebody else doing it wrong, without | 0214 Oslo | comment. | NORWAY | | Tlf: +47 24 15 38 90 | | =46ax: +47 24 15 38 91 | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+