Srs:

Estou tentando executar um count mais está muito lento, já sei de todas as
restrições (mvcc) e problemas dele, já tentei de tudo, quem sabe um sql
tunning resolva meu problema.

Segue os dados abaixo

select count(*) as y0_ from livro this_ inner join autor a1_ on
this_.autor_id=a1_.id inner join editora e3_ on this_.editora_id=e3_.id
inner join titulo t2_ on this_.titulo_id=t2_.id inner join estante_pessoa
ep5_ on this_.estante_pessoa_id=ep5_.id inner join pessoa p4_ on
this_.pessoa_id=p4_.id where
((plainto_tsquery('simple', 'casamento') @@ t2_.idx_titulo_nome) OR
(plainto_tsquery('simple', 'casamento' ) @@ a1_.idx_autor_nome))
and p4_.ativo=true and this_.estoque=1

Explain http://explain.depesz.com/s/Y3E

<http://explain.depesz.com/s/Y3E>Tentei retirar os ids da tabela autor e
titulo para uso de indices, mais nao resolveu

select count(*) as y0_ from livro this_ inner join autor a1_ on
this_.autor_id=a1_.id inner join editora e3_ on this_.editora_id=e3_.id
inner join titulo t2_ on this_.titulo_id=t2_.id inner join estante_pessoa
ep5_ on this_.estante_pessoa_id=ep5_.id inner join pessoa p4_ on
this_.pessoa_id=p4_.id where (((this_.titulo_id in (Select id from titulo
WHERE (plainto_tsquery('simple', 'casamento') @@ idx_titulo_nome)  )) OR
(this_.autor_id in (Select id from autor WHERE (plainto_tsquery('simple',
'casamento' ) @@ idx_autor_nome) ) ))) and p4_.ativo=true and
this_.estoque=1

Explani: http://explain.depesz.com/s/u7M

Pelo que pude ver meu problema está no agreggate que demora mutio, e
normalmente pode se solucionar no hardware.
Posto aqui minhas info de hardware

i7, 12GB memoria,hd Sata (sem raid)
postgreql-8.4.5-1.el5_5.1

Meu postgresql.conf
shared_buffers = 3GB                    # min 128kB
work_mem = 5000MB                               # min 64kB
maintenance_work_mem = 200MB            # min 1MB
wal_buffers = 1MB                       # min 32kB
enable_seqscan = on
random_page_cost = 1.0                  # same scale as above
effective_cache_size = 2GB

Minhas tabelas
Autor e livro sao iguais

 \d autor
                                    Table "public.autor"
     Column     |         Type          |                     Modifiers

----------------+-----------------------+----------------------------------------------------
 id             | bigint                | not null default
nextval('autor_id_seq'::regclass)
 nome           | character varying(80) | not null
 idx_autor_nome | tsvector              |
Indexes:
    "autor_pkey" PRIMARY KEY, btree (id)
    "autor_nome_key" UNIQUE, btree (nome)
    "idx_autor_id" btree (id)
    "idx_autor_idx_nome" gist (idx_autor_nome)
    "idx_autor_nome" btree (nome) CLUSTER
    "idx_autor_nome_lower" btree (lower(nome::text))
Referenced by:
    TABLE "livro" CONSTRAINT "fk6236e961f15dad5" FOREIGN KEY (autor_id)
REFERENCES autor(id)
    TABLE "lista_livro" CONSTRAINT "lista_livro_autor_id_pkey" FOREIGN KEY
(autor_id) REFERENCES autor(id)
Triggers:
    tsvectorupdate_autor BEFORE INSERT OR UPDATE ON autor FOR EACH ROW
EXECUTE PROCEDURE tsearch2('idx_autor_nome', 'nome')

\d livro

 Column       |            Type             |                     Modifiers

-------------------+-----------------------------+----------------------------------------------------
 id                | bigint                      | not null default
nextval('livro_id_seq'::regclass)
 estoque           | integer                     | not null
 pessoa_id         | character varying(7)        | not null
 titulo_id         | bigint                      | not null
 autor_id          | bigint                      | not null
 estante_pessoa_id | bigint                      | not null
 cod_interno       | bigint                      |
Indexes:
    "livro_pkey" PRIMARY KEY, btree (id)
    "idx_livro_autor_id" btree (autor_id)
    "idx_livro_autor_titulo" btree (autor_id, titulo_id)
    "idx_livro_autor_titulo_id" btree (autor_id, titulo_id)
    "idx_livro_cod_interno_pessoa" btree (cod_interno, pessoa_id)
    "idx_livro_editora_id" btree (editora_id)
    "idx_livro_estantepessoa_id" btree (estante_pessoa_id)
    "idx_livro_estantepessoa_id_hash" hash (estante_pessoa_id)
    "idx_livro_existe" btree (editora_id, titulo_id, autor_id, pessoa_id)
    "idx_livro_id" btree (id)
    "idx_livro_pessoa" btree (pessoa_id)
    "idx_livro_titulo_id" btree (titulo_id)

Referenced by:
    TABLE "cesto" CONSTRAINT "cesto_livro_id_fkey" FOREIGN KEY (livro_id)
REFERENCES livro(id) ON DELETE CASCADE
    TABLE "compra_livro" CONSTRAINT "compra_livro_livro_id_fkey" FOREIGN KEY
(livro_id) REFERENCES livro(id)
    TABLE "inventario_livro" CONSTRAINT "inventario_livro_livro_id_fkey"
FOREIGN KEY (livro_id) REFERENCES livro(id)


<http://explain.depesz.com/s/u7M>--

Giancarlo Rubio
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a