Re: [GENERAL] Generic search - Mailing list pgsql-general
From | Sferacarta Software |
---|---|
Subject | Re: [GENERAL] Generic search |
Date | |
Msg-id | 19617.981204@bo.nettuno.it Whole thread Raw |
In response to | Generic search ("jerome doucerain" <jerome.doucerain@chu-nimes.fr>) |
List | pgsql-general |
Hello Jerome, venerdì, 4 dicembre 98, you wrote: jd> Hi, jd> As it seems jd> Postgres doesn't use index when the where clause contains "like" operator. jd> It only uses it in case of "=" operator. jd> for example : test=>> create table essai (nom varchar(50)); jd> CREATE test=>> create index essai_nom on essai (nom); jd> CREATE test=>> \d essai jd> Table = essai jd> +----------------------------------+----------------------------------+----- jd> --+ jd> | Field | Type | jd> Length| jd> +----------------------------------+----------------------------------+----- jd> --+ jd> | nom | varchar() | jd> 50 | jd> +----------------------------------+----------------------------------+----- jd> --+ test=>> \d essai_nom jd> Table = essai_nom jd> +----------------------------------+----------------------------------+----- jd> --+ jd> | Field | Type | jd> Length| jd> +----------------------------------+----------------------------------+----- jd> --+ jd> | nom | varchar() | jd> -4 | jd> +----------------------------------+----------------------------------+----- jd> --+ test=>> explain select * from essai where nom='abc'; jd> NOTICE: QUERY PLAN: jd> Index Scan using essai_nom on essai (cost=0.00 size=0 width=12) jd> EXPLAIN test=>> explain select * from essai where nom like 'a%'; jd> NOTICE: QUERY PLAN: jd> Seq Scan on essai (cost=0.00 size=0 width=12) jd> EXPLAIN jd> Postgres 6.4 Linux 2.0.34. jd> So I'm looking for a way to get quite good performances with generic jd> search. jd> Thank you for advices. jd> Jerome. How many records do you have in this table ? If you have only few records PostgreSQL doesn't use index because it is faster to read in sequential mode. Try to insert a lot of records. It works, look: hygea=> explain select * from comuni where nome = 'A%'; NOTICE: QUERY PLAN: Index Scan using nome_comune_idx on comuni (cost=2.05 size=2 width=84) ^^^^^ EXPLAIN hygea=> explain select * from comuni where nome > 'A%'; NOTICE: QUERY PLAN: Index Scan using nome_comune_idx on comuni (cost=215.03 size=2781 width=84) ^^^^^ EXPLAIN hygea=> explain select * from comuni where nome <> 'A%'; NOTICE: QUERY PLAN: Seq Scan on comuni (cost=427.29 size=8342 width=84) EXPLAIN hygea=> explain select * from comuni where nome < 'A%'; NOTICE: QUERY PLAN: Index Scan using nome_comune_idx on comuni (cost=215.03 size=2781 width=84) ^^^^^ EXPLAIN hygea=> explain select * from comuni where nome like '%A%'; NOTICE: QUERY PLAN: Seq Scan on comuni (cost=427.29 size=2 width=84) ^^^ Pg uses index with LIKE only if the first char is not a special char. -Jose'-
pgsql-general by date: