X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 1B9E7D1C91D for ; Fri, 25 Jul 2003 17:29:36 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 54729-08 for ; Fri, 25 Jul 2003 14:28:48 -0300 (ADT) Received: from davinci.ethosmedia.com (unknown [209.10.40.251]) by svr1.postgresql.org (Postfix) with ESMTP id 069BBD1C92A for ; Fri, 25 Jul 2003 14:29:34 -0300 (ADT) Received: from [66.219.92.2] (HELO temoku) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.0.2) with ESMTP id 3255824; Fri, 25 Jul 2003 10:29:46 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: Franco Bruno Borghesi , pgsql-performance@postgresql.org Subject: Re: index questions Date: Fri, 25 Jul 2003 10:28:27 -0700 User-Agent: KMail/1.4.3 References: <1059151963.1183.14.camel@taz> In-Reply-To: <1059151963.1183.14.camel@taz> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200307251028.27261.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/421 X-Sequence-Number: 2714 Franco, > CREATE INDEX sales_k1 ON sales(clientId, branchId, productId, > employeeId, saleDate, price, qty); A 7-column index is unlikely to be effective -- the index will be almost as= =20 large as the table. Try indexing only the first 3-4 columns instead.=20= =20 > I want to make a function that returns the FIRS saleId of the sale that > matches some conditions. I will > always receive the Client Id, but not always the other arguments (sent > as NULLs). Well, keep in mind that your multi-column index will only be useful if all= =20 columns are queried starting from the left. That is, the index will be=20 ignored if you have a "where productId =3D x" without a "where branchid =3D= y". > CREATE OR REPLACE FUNCTION findSale(INTEGER, INTEGER, INTEGER, INTEGER) > RETURNS INTEGER AS ' > DECLARE > a_clientId ALIAS FOR $1; > a_branchId ALIAS FOR $1; > a_productId ALIAS FOR $1; > a_employeeId ALIAS FOR $1; Your aliases are wrong here. > branchId=3Dcoalesce(a_branchId, branchId) AND /*branchId is null? > anything will be ok*/ > productId=3Dcoalesce(a_productId, productId) AND /*productId is > null? anything will be ok*/ On a very large table this will be very inefficient. you'll be comparing = the=20 productid, for example, even if no productid is passed ... and the index=20 won't do you any good because the planner should figure out that 100% of ro= ws=20 match the condition.=20=20 Instead, I recommend that you build up a dynamic query as a string and then= =20 pass only the conditions sent by the user. You can then EXECUTE the query= =20 and loop through it for a result. Of course, YMMV. My approach will require you to create more indexes whic= h=20 could be a problem if you have limited disk space. --=20 -Josh Berkus Aglio Database Solutions San Francisco