X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C7C6252AAD for ; Wed, 20 Apr 2005 18:06:27 -0300 (ADT) 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 43633-05 for ; Wed, 20 Apr 2005 21:06:19 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id E0CBC538C4 for ; Wed, 20 Apr 2005 18:06:18 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j3KL6GEq004711; Wed, 20 Apr 2005 17:06:16 -0400 (EDT) To: "Tambet Matiisen" Cc: =?iso-8859-4?Q?K=D6PFERL_Robert?= , pgsql-sql@postgresql.org Subject: Re: Function to either return one or all records In-reply-to: References: Comments: In-reply-to "Tambet Matiisen" message dated "Wed, 20 Apr 2005 23:02:53 +0300" Date: Wed, 20 Apr 2005 17:06:16 -0400 Message-ID: <4710.1114031176@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/308 X-Sequence-Number: 21201 "Tambet Matiisen" writes: > Standard technique is to rewrite OR queries to UNION queries. I believe PostgreSQL optimizer does not do that automatically. So you could try instead: > select * from id_bal_tbl where $1 is null > union all > select * from id_bal_tbl where id = $1; ... but that won't do anything to solve the performance problem. Doesn't help for the second select to use an index, if the first one grovels over the whole table anyway ... regards, tom lane