X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id D175D9DCD06 for ; Fri, 17 Mar 2006 02:25:31 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80121-05 for ; Fri, 17 Mar 2006 02:25:31 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from pillette.com (adsl-67-119-5-202.dsl.snfc21.pacbell.net [67.119.5.202]) by postgresql.org (Postfix) with ESMTP id 2C42C9DCD2D for ; Fri, 17 Mar 2006 02:25:28 -0400 (AST) Received: (from andrew@localhost) by pillette.com (8.11.6/8.11.6) id k2H6PJ926955; Thu, 16 Mar 2006 22:25:19 -0800 Date: Thu, 16 Mar 2006 22:25:19 -0800 From: andrew@pillette.com Message-Id: <200603170625.k2H6PJ926955@pillette.com> Subject: Re: Indexes with descending date columns To: theo@flame.co.za Cc: pgsql-performance@postgresql.org X-Originating-IP: 209.125.147.195 X-Mailer: Webmin 0.940 MIME-Version: 1.0 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.532 required=5 tests=[AWL=-0.018, NO_REAL_NAME=0.55] X-Spam-Score: 0.532 X-Spam-Level: Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Archive-Number: 200603/264 X-Sequence-Number: 17701 > I have a performance problem when traversing a table in index order with > multiple columns including a date column in date reverse order. Below > follows a simplified description of the table, the index and the > associated query > > \d prcdedit > prcdedit_prcd | character(20) | > prcdedit_date | timestamp without time zone | > > Indexes: > "prcdedit_idx" btree (prcdedit_prcd, prcdedit_date) Depending on how you use the table, there are three possible solutions. First, if it makes sense in the domain, using an ORDER BY where _both_ columns are used descending will make PG search the index in reverse and will be just as fast as when both as searched by the default ascending. Second possibility: Create a dummy column whose value depends on the negative of prcdedit_date, e.g., -extract(epoch from prcdedit_date), keep the dummy column in sync with the original column using triggers, and rewrite your queries to use ORDER BY prcdedit_prod, dummy_column. Third: Create an index on a function which sorts in the order you want, and then always sort using the function index (you could use the -extract(epoch...) gimmick for that, among other possibilities.) HTH.