Thread: Seeking for the fore-part of the key

Seeking for the fore-part of the key

From
"Havasvölgyi Ottó"
Date:
Hi,<br /><br />Let's assume I have a table (called tbl) with a column 'name' and an index on it.<br />How to optimally
findrecords whose name column begins with - say - "Pre"?<br />I know it can be done with LIKE (WHERE name LIKE 'Pre%'),
butwill this use the index mentioned above to optimize the search?<br /><br />Thanks,<br />Otto<br /> 

Re: Seeking for the fore-part of the key

From
Andreas Kretschmer
Date:
Havasvölgyi Ottó <havasvolgyi.otto@gmail.com> 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
HeapScan 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.019rows=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°