From: Rodrigo De León <rdeleonp@gmail.com>
To: Andreas Joseph Krogh <andreak@officenet.no>
Cc: pgsql-sql@postgresql.org
Subject: Re: PG won't use index on ORDER BY <expression>
Date: Thu, 9 Aug 2007 15:57:35 -0500
Message-ID: <a55915760708091357m4632c952s10bdafd235765941@mail.gmail.com> (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 8/9/07, Andreas Joseph Krogh <andreak@officenet.no> wrote:
> Ooops, just fugured that out. But - it still doesn't use the index if I remove
> the "varchar_pattern_ops".
Huh?
CREATE INDEX person_lowerfullname_idx
ON person
((lower(COALESCE(firstname, '')) || lower(COALESCE(lastname, ''))));
EXPLAIN ANALYZE select id from person order by
(lower(COALESCE(firstname, '')) ||
lower(COALESCE(lastname, ''))) ASC limit 1;
Limit (cost=0.00..0.08 rows=1 width=68) (actual time=0.030..0.030
rows=0 loops=1)
-> Index Scan using person_lowerfullname_idx on person
(cost=0.00..62.25 rows=800 width=68) (actual time=0.018..0.018 rows=0
loops=1)
Total runtime: 0.318 ms
EXPLAIN ANALYZE select id from person
where (lower(COALESCE(firstname, '')) ||
lower(COALESCE(lastname, ''))) like 'A%'
order by (lower(COALESCE(firstname, '')) || lower(COALESCE(lastname, '')))
ASC limit 1;
Limit (cost=0.01..5.10 rows=1 width=68) (actual time=0.038..0.038
rows=0 loops=1)
-> Index Scan using person_lowerfullname_idx on person
(cost=0.01..20.34 rows=4 width=68) (actual time=0.032..0.032 rows=0
loops=1)
Index Cond: (((lower((COALESCE(firstname, ''::character
varying))::text) || lower((COALESCE(lastname, ''::character
varying))::text)) >= 'A'::text) AND ((lower((COALESCE(firstname,
''::character varying))::text) || lower((COALESCE(lastname,
''::character varying))::text)) < 'B'::text))
Filter: ((lower((COALESCE(firstname, ''::character
varying))::text) || lower((COALESCE(lastname, ''::character
varying))::text)) ~~ 'A%'::text)
Total runtime: 0.138 ms
Works for me.
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: rdeleonp@gmail.com, andreak@officenet.no
Subject: Re: PG won't use index on ORDER BY <expression>
In-Reply-To: <a55915760708091357m4632c952s10bdafd235765941@mail.gmail.com>
* 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