Re: PG won't use index on ORDER BY - Mailing list pgsql-sql

From Rodrigo De León
Subject Re: PG won't use index on ORDER BY
Date
Msg-id a55915760708091357m4632c952s10bdafd235765941@mail.gmail.com
Whole thread Raw
In response to Re: PG won't use index on ORDER BY  (Andreas Joseph Krogh <andreak@officenet.no>)
Responses Re: PG won't use index on ORDER BY  (Andreas Joseph Krogh <andreak@officenet.no>)
List pgsql-sql
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.


pgsql-sql by date:

Previous
From: Andreas Joseph Krogh
Date:
Subject: Re: PG won't use index on ORDER BY
Next
From: Andreas Joseph Krogh
Date:
Subject: Re: PG won't use index on ORDER BY