X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 13358D8B0E for ; Sat, 3 Sep 2005 19:11:08 -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 95203-01 for ; Sat, 3 Sep 2005 22:11:04 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id A6784D796E for ; Sat, 3 Sep 2005 19:11:02 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j83MB1JU030943 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 3 Sep 2005 16:11:03 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j83MB0mC003125; Sat, 3 Sep 2005 16:11:00 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j83MB0sD003124; Sat, 3 Sep 2005 16:11:00 -0600 (MDT) (envelope-from mfuhr) Date: Sat, 3 Sep 2005 16:11:00 -0600 From: Michael Fuhr To: Carlos Benkendorf Cc: pgsql-performance@postgresql.org Subject: Re: Improving performance of a query Message-ID: <20050903221100.GA2985@winnie.fuhr.org> References: <20050903210227.75394.qmail@web35508.mail.mud.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050903210227.75394.qmail@web35508.mail.mud.yahoo.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 required=5 tests=[AWL=0.005] X-Spam-Level: X-Archive-Number: 200509/66 X-Sequence-Number: 14374 On Sat, Sep 03, 2005 at 09:02:27PM +0000, Carlos Benkendorf wrote: > Is there a way to improve the performance of the following query? > > SELECT * FROM SSIRRA where > (YEAR = 2004 and CUSTOMER = 0000000004 and CODE = 00 and PART >= 00) or > (YEAR = 2004 and CUSTOMER = 0000000004 and CODE > 00) or > (YEAR = 2004 and CUSTOMER > 0000000004) or > (YEAR > 2004) Could you post the EXPLAIN ANALYZE output of the query? It might also be useful to see the EXPLAIN ANALYZE output for each of those WHERE conditions individually. Also, what are the table and index definitions? How many rows are in the table? What version of PostgreSQL are you using? Do you vacuum and analyze regularly? In simple tests in 8.0.3 with random data -- which almost certainly has a different distribution than yours -- I see about a 10% improvement with a multi-column index on (year, customer, code, part) over using single-column indexes on each of those columns. Various multi-column indexes on two or three of the columns gave worse performance than single-column indexes. Your results will probably vary, however. In my tests, 8.1beta1 with its bitmap scans was about 20-25% faster than 8.0.3 with single-column indexes and about 35% faster with a four-column index. 8.1beta1's use of a four-column index was about 45% faster than 8.0.3's use of single-column indexes. Don't trust these numbers too much, though -- I simply inserted 20,000 random records into a table and ran the above query. -- Michael Fuhr