On 07-08-2015 16:19, Fernando Cambiaghi wrote:
SELECT idx_scan, indexrelname FROM pg_stat_user_indexes WHERE relname =
'cliente';
215072;  "pk_cliente"
14;         "idx_cliente_inclusao"
14;         "idx_filial_data_atualizacao"
9;           "idx_nr_cpf_cgc"
0;           "idx_rg"
0;           "idx_nm_cliente"

Remova os índices com pouca utilização.

Com isso eu gostaria de saber se essa é a única e/ou melhor solução para
o índice de colunas tipo varchar onde a busca é realizada frequentemente
com LIKE? Caso não, qual outra opção poderia ser aplicada?

Não é a única. Se você criar um índice GIN, você pode usar a mesma consulta acima que ele irá usar o índice. Uma vantagem do índice GIN em relação ao B-Tree é que você pode usar o % no início da expressão regular. Veja:

congresso=# create extension pg_trgm;
CREATE EXTENSION
congresso=# create index nome_gin on tab_municipios using gin (nome gin_trgm_ops);
CREATE INDEX
congresso=# analyze tab_municipios;
ANALYZE
congresso=# explain analyze select * from tab_municipios where nome like 'PALM%'; QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on tab_municipios (cost=36.00..40.02 rows=1 width=27) (actual time=0.161..0.161 rows=0 loops=1)
   Recheck Cond: ((nome)::text ~~ 'PALM%'::text)
   Rows Removed by Index Recheck: 40
   Heap Blocks: exact=24
-> Bitmap Index Scan on nome_gin (cost=0.00..36.00 rows=1 width=0) (actual time=0.089..0.089 rows=40 loops=1)
         Index Cond: ((nome)::text ~~ 'PALM%'::text)
 Planning time: 0.388 ms
 Execution time: 0.205 ms
(8 registros)

congresso=# explain analyze select * from tab_municipios where nome like '%PALM%'; QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on tab_municipios (cost=20.00..24.02 rows=1 width=27) (actual time=0.133..0.133 rows=0 loops=1)
   Recheck Cond: ((nome)::text ~~ '%PALM%'::text)
   Rows Removed by Index Recheck: 40
   Heap Blocks: exact=24
-> Bitmap Index Scan on nome_gin (cost=0.00..20.00 rows=1 width=0) (actual time=0.052..0.052 rows=40 loops=1)
         Index Cond: ((nome)::text ~~ '%PALM%'::text)
 Planning time: 0.228 ms
 Execution time: 0.177 ms
(8 registros)
congresso=# drop index nome_gin;
DROP INDEX

Usando o índice B-Tree ele *nem* sempre vai utilizar o índice.

congresso=# create index nome_btree on tab_municipios(nome varchar_pattern_ops);
CREATE INDEX
congresso=# analyze tab_municipios;
ANALYZE
congresso=# explain analyze select * from tab_municipios where nome like 'PALM%'; QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Index Scan using nome_btree on tab_municipios (cost=0.28..8.30 rows=1 width=27) (actual time=0.025..0.025 rows=0 loops=1) Index Cond: (((nome)::text ~>=~ 'PALM'::text) AND ((nome)::text ~<~ 'PALN'::text))
   Filter: ((nome)::text ~~ 'PALM%'::text)
 Planning time: 0.515 ms
 Execution time: 0.060 ms
(5 registros)

congresso=# explain analyze select * from tab_municipios where nome like '%PALM%'; QUERY PLAN
-----------------------------------------------------------------------------------------------------------
Seq Scan on tab_municipios (cost=0.00..109.55 rows=1 width=27) (actual time=2.290..2.290 rows=0 loops=1)
   Filter: ((nome)::text ~~ '%PALM%'::text)
   Rows Removed by Filter: 5564
 Planning time: 0.162 ms
 Execution time: 2.319 ms
(5 registros)


--
   Euler Taveira                   Timbira - http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a