From: Andreas Joseph Krogh <andreak@officenet.no>
To: pgsql-sql@postgresql.org
Subject: Re: PG won't use index on ORDER BY <expression>
Date: Fri, 10 Aug 2007 00:15:33 +0200
Message-ID: <200708100015.34117.andreak@officenet.no> (raw)
In-Reply-To: <200708092238.46284.andreak@officenet.no>
References: <200708092018.44111.andreak@officenet.no>
<87fy2s1nrd.fsf@oxford.xeocode.com>
<200708092238.46284.andreak@officenet.no>
On Thursday 09 August 2007 22:38:46 Andreas Joseph Krogh wrote:
> On Thursday 09 August 2007 22:00:54 Gregory Stark wrote:
> > "Andreas Joseph Krogh" <andreak@officenet.no> 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
> remove the "varchar_pattern_ops". I solved it by adding a function:
>
> CREATE OR REPLACE FUNCTION concat_lower(varchar, varchar) RETURNS varchar
> AS $$
> 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,
> lastname));
>
> Another question then: Why doesn't "varchar_pattern_ops" handle ordering?
> This means I need 2 indexes on the columns I want to match with LIKE and
> ORDER BY. Just doesn't seem right to need 2 "similar" indexes...
Hmm, one more question:
If I want to ORDER BY "created" too, the index is not used anymore:
-- This uses index:
EXPLAIN ANALYZE select firstname, lastname from person order by
concat_lower(firstname, lastname) ASC limit 10;
-- This doesn't
EXPLAIN ANALYZE select firstname, lastname from person order by
concat_lower(firstname, lastname) ASC, created DESC limit 10;
I figured out that it's because they have different sort-order (ASC/DESC). If
both are ASC, the index is used.
Is there a way around this?
--
Andreas Joseph Krogh <andreak@officenet.no>
Senior Software Developer / Manager
------------------------+---------------------------------------------+
OfficeNet AS | The most difficult thing in the world is to |
Karenslyst Allé 11 | know how to do a thing and to watch |
PO. Box 529 Skøyen | somebody else doing it wrong, without |
0214 Oslo | comment. |
NORWAY | |
Tlf: +47 24 15 38 90 | |
Fax: +47 24 15 38 91 | |
Mobile: +47 909 56 963 | |
------------------------+---------------------------------------------+
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: andreak@officenet.no
Subject: Re: PG won't use index on ORDER BY <expression>
In-Reply-To: <200708100015.34117.andreak@officenet.no>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox