Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 19DD19FBEB8 for ; Thu, 9 Aug 2007 17:39:00 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 57297-10 for ; Thu, 9 Aug 2007 17:38:44 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from andreak-laptop.officenet.no (79.84-48-119.nextgentel.com [84.48.119.79]) by postgresql.org (Postfix) with ESMTP id 9DB6E9FBF63 for ; Thu, 9 Aug 2007 17:38:47 -0300 (ADT) Received: from localhost ([127.0.0.1]) by andreak-laptop.officenet.no with esmtp (Exim 4.63) (envelope-from ) id 1IJEmM-00044Z-LN; Thu, 09 Aug 2007 22:38:46 +0200 From: Andreas Joseph Krogh Organization: OfficeNet AS To: Gregory Stark Subject: Re: PG won't use index on ORDER BY Date: Thu, 9 Aug 2007 22:38:46 +0200 User-Agent: KMail/1.9.6 Cc: pgsql-sql@postgresql.org References: <200708092018.44111.andreak@officenet.no> <87fy2s1nrd.fsf@oxford.xeocode.com> In-Reply-To: <87fy2s1nrd.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200708092238.46284.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/57 X-Sequence-Number: 28995 On Thursday 09 August 2007 22:00:54 Gregory Stark wrote: > "Andreas Joseph Krogh" writes: > > I create an index: > > CREATE INDEX person_lowerfullname_idx ON > > person((lower(COALESCE(firstname, '')) || lower(COALESCE(lastname, ''))) > > varchar_pattern_ops); > > Why are you declaring it using the varchar_pattern_ops? > > The default operator set is the one you want for handling ordering. The > pattern_ops operator set is for handling things like x LIKE 'foo%' Ooops, just fugured that out. But - it still doesn't use the index if I rem= ove=20 the "varchar_pattern_ops". I solved it by adding a function: CREATE OR REPLACE FUNCTION concat_lower(varchar, varchar) RETURNS varchar A= S=20 $$ SELECT lower(coalesce($1, '')) || lower(coalesce($2, '')) $$ LANGUAGE SQL IMMUTABLE; And than creating an index: CREATE INDEX person_lowerfullname_idx ON person(concat_lower(firstname,=20 lastname)); Another question then: Why doesn't "varchar_pattern_ops" handle ordering? T= his=20 means I need 2 indexes on the columns I want to match with LIKE and ORDER B= Y.=20 Just doesn't seem right to need 2 "similar" indexes... =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Karenslyst All=E9 11 | know how to do a thing and to watch | PO. Box 529 Sk=F8yen | somebody else doing it wrong, without | 0214 Oslo | comment. | NORWAY | | Tlf: +47 24 15 38 90 | | =46ax: +47 24 15 38 91 | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+