Re: Indexes with descending date columns - Mailing list pgsql-performance

From andrew@pillette.com
Subject Re: Indexes with descending date columns
Date
Msg-id 200603170625.k2H6PJ926955@pillette.com
Whole thread Raw
In response to Indexes with descending date columns  (Theo Kramer <theo@flame.co.za>)
Responses Re: Indexes with descending date columns
List pgsql-performance
> 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
theindex 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
fromprcdedit_date), keep the dummy column in sync with the original column using triggers, and rewrite your queries to
useORDER 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
(youcould use the -extract(epoch...) gimmick for that, among other possibilities.) 

HTH.

pgsql-performance by date:

Previous
From: Josh Berkus
Date:
Subject: Re: 1 TB of memory
Next
From: Guillaume Cottenceau
Date:
Subject: planner with index scan cost way off actual cost, advices to tweak cost constants?