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 7F78C9DC833 for ; Wed, 18 Jan 2006 10:06:08 -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 50828-03 for ; Wed, 18 Jan 2006 10:06:12 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from bob.planeti.biz (unknown [66.98.130.45]) by postgresql.org (Postfix) with ESMTP id 244619DC81D for ; Wed, 18 Jan 2006 10:06:05 -0400 (AST) Received: from fatchubby (cpe-071-070-134-248.nc.res.rr.com [71.70.134.248]) by bob.planeti.biz (8.12.10/8.12.5) with SMTP id k0IDvJfR018003 for ; Wed, 18 Jan 2006 08:57:19 -0500 From: J@Planeti.Biz Message-ID: <00d801c61c38$52863030$81300d05@fatchubby> To: References: <001401c61bf3$aec1fef0$7f00a8c0@kicommunication.com> Subject: Re: Multiple Order By Criteria Date: Wed, 18 Jan 2006 09:06:05 -0500 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2527 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.108 required=5 tests=[AWL=0.558, NO_REAL_NAME=0.55] X-Spam-Score: 1.108 X-Spam-Level: * X-Archive-Number: 200601/291 X-Sequence-Number: 16769 I have the answer I've been looking for and I'd like to share with all. After help from you guys, it appeared that the real issue was using an index for my order by X DESC clauses. For some reason that doesn't make good sense, postgres doesn't support this, when it kinda should automatically. Take the following end of an SQL statement. order by col1 DESC col2 ASC col3 ASC The first thing I learned is that you need an index that contains all these columns in it, in this order. If one of them has DESC then you have to create a function / operator class for each data type, in this case let's assume it's an int4. So, first thing you do is create a function that you're going to use in your operator: create function int4_revcmp(int4,int4) // --> cal the function whatever you want returns int4 as 'select $2 - $1' language sql; Then you make your operator class. CREATE OPERATOR CLASS int4_revop FOR TYPE int4 USING btree AS OPERATOR 1 > , OPERATOR 2 >= , OPERATOR 3 = , OPERATOR 4 <= , OPERATOR 5 < , FUNCTION 1 int4_revcmp(int4, int4); // --> must be the name of your function you created. Then when you make your index create index rev_idx on table using btree( col1 int4_revop, // --> must be name of operator class you defined. col2, col3 ); What I don't understand is how to make this function / operator class work with a text datatype. I tried interchanging the int4 with char and text and postgres didn't like the (as 'select $2 - $1') in the function, which I can kinda understand. Since I'm slighlty above my head at this point, I don't really know how to do it. Does any smart people here know how ?