Seeking for the fore-part of the key

Поиск
Список
Период
Сортировка
Искать

Seeking for the fore-part of the key

От:
"Havasvölgyi Ottó" <havasvolgyi.otto@gmail.com>
Дата:
Hi,

Let's assume I have a table (called tbl) with a column 'name' and an index on it.
How to optimally find records whose name column begins with - say - "Pre"?
I know it can be done with LIKE (WHERE name LIKE 'Pre%'), but will this use the index mentioned above to optimize the search?

Thanks,
Otto

Re: Seeking for the fore-part of the key

От:
Andreas Kretschmer <akretschmer@spamfence.net>
Дата:
Havasvölgyi Ottó  schrieb:

> Hi,
> 
> Let's assume I have a table (called tbl) with a column 'name' and an index on
> it.
> How to optimally find records whose name column begins with - say - "Pre"?
> I know it can be done with LIKE (WHERE name LIKE 'Pre%'), but will this use the
> index mentioned above to optimize the search?

for instance:


test=*# \d words Tabelle »public.words«Spalte | Typ  | Attribute
--------+------+-----------w      | text |
Indexe:   »idx_words_w« btree (lower(w) text_pattern_ops)

test=*# explain analyse select * from words where lower(w) like lower('foo%');                                                      QUERY PLAN
------------------------------------------------------------------------------------------------------------------------Bitmap Heap Scan on words  (cost=40.65..1779.81 rows=1595 width=13) (actual time=0.021..0.021 rows=0 loops=1)  Filter: (lower(w) ~~ 'foo%'::text)  ->  Bitmap Index Scan on idx_words_w  (cost=0.00..40.25 rows=1595 width=0) (actual time=0.019..0.019 rows=0 loops=1)        Index Cond: ((lower(w) ~>=~ 'foo'::text) AND (lower(w) ~<~ 'fop'::text))Total runtime: 0.047 ms
(5 Zeilen)



Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

FAQ