Pessoal, estou tendo o explain abaixo para uma view. O que não entendo é por 
que o pg não está usando a chave primeira para fazer um join com a tabela de 
compras (Seq Scan on compras c  (cost=0.00..5125.69 rows=79569 width=26)). 
Alguém pode me orientar?

Jean Domingues

"QUERY PLAN"
"Sort  (cost=54371.15..54378.52 rows=2946 width=641)"
"  Sort Key: nfe.numero_nf, nfe.id"
"  ->  Hash Left Join  (cost=27995.71..54171.94 rows=2946 width=522)"
"        Hash Cond: (nfe.id_contingencia = ctg.id)"
"        ->  Hash Left Join  (cost=27994.67..53371.78 rows=2946 width=520)"
"              Hash Cond: (v.id_romaneio = r1.id)"
"              ->  Hash Left Join  (cost=26146.90..51454.25 rows=2946 
width=508)"
"                    Hash Cond: (c.id_romaneio_devolucao = r2.id)"
"                    ->  Hash Left Join  (cost=24299.13..49543.67 rows=2946 
width=496)"
"                          Hash Cond: (nfe.id_func_cancelamento = t3.id)"
"                          ->  Nested Loop  (cost=23519.68..48731.05 rows=2946 
width=474)"
"                                ->  Seq Scan on nfe_filas f  (cost=0.00..1.04 
rows=1 width=11)"
"                                      Filter: (id = 1)"
"                                ->  Hash Left Join  (cost=23519.68..48700.55 
rows=2946 width=465)"
"                                      Hash Cond: (nfe.id_nat_operacao = 
nop1.id)"
"                                      ->  Hash Left Join  
(cost=23518.19..48684.77 rows=2946 width=441)"
"                                            Hash Cond: (nfe.id_terceiro = 
t1.id)"
"                                            ->  Nested Loop Left Join  
(cost=22738.74..47842.72 rows=2946 width=396)"
"                                                  ->  Hash Left Join  
(cost=22738.74..28418.97 rows=2946 width=377)"
"                                                        Hash Cond: 
(nfe.id_func_emissao = t2.id)"
"                                                        ->  Hash Right Join  
(cost=21959.29..27582.41 rows=2946 width=355)"
"                                                              Hash Cond: (c.id 
= nfe.id_compra)"

"                                                              ->  Seq Scan on 
compras c  (cost=0.00..5125.69 rows=79569 width=26)"

"                                                              ->  Hash  
(cost=21922.47..21922.47 rows=2946 width=337)"
"                                                                    ->  Bitmap 
Heap Scan on nfe  (cost=209.10..21922.47 rows=2946 width=337)"
"                                                                          
Recheck Cond: (((id_empresa = 1) AND (dh_envio_email IS NULL)) OR ((id_empresa 
= 1) AND (dh_cancelamento IS NOT NULL)))"
"                                                                          
Filter: ((((dh_cancelamento IS NULL) AND (dh_envio_email IS NULL) AND (NOT 
nao_enviar_email)) OR ((dh_cancelamento IS NOT NULL) AND (dh_envio_email_canc 
IS NULL) AND (NOT nao_enviar_email_canc))) AND ((COALESCE(email, ''::character 
varying))::text <> ''::text) AND (id_fila = 1) AND (status_retorno = 
'1'::bpchar))"
"                                                                          ->  
BitmapOr  (cost=209.10..209.10 rows=7464 width=0)"
"                                                                               
 ->  Bitmap Index Scan on nfe_idx12  (cost=0.00..203.05 rows=7455 width=0)"
"                                                                               
       Index Cond: ((id_empresa = 1) AND (dh_envio_email IS NULL))"
"                                                                               
 ->  Bitmap Index Scan on nfe_idx7  (cost=0.00..4.58 rows=9 width=0)"
"                                                                               
       Index Cond: ((id_empresa = 1) AND (dh_cancelamento IS NOT NULL))"
"                                                        ->  Hash  
(cost=600.31..600.31 rows=14331 width=30)"
"                                                              ->  Seq Scan on 
terceiros t2  (cost=0.00..600.31 rows=14331 width=30)"
"                                                  ->  Index Scan using 
vendas_pkey on vendas v  (cost=0.00..6.58 rows=1 width=27)"
"                                                        Index Cond: (id = 
nfe.id_venda)"
"                                            ->  Hash  (cost=600.31..600.31 
rows=14331 width=49)"
"                                                  ->  Seq Scan on terceiros t1 
 (cost=0.00..600.31 rows=14331 width=49)"
"                                      ->  Hash  (cost=1.22..1.22 rows=22 
width=32)"
"                                            ->  Seq Scan on 
naturezas_operacoes nop1  (cost=0.00..1.22 rows=22 width=32)"
"                          ->  Hash  (cost=600.31..600.31 rows=14331 width=30)"
"                                ->  Seq Scan on terceiros t3  
(cost=0.00..600.31 rows=14331 width=30)"
"                    ->  Hash  (cost=1621.23..1621.23 rows=18123 width=20)"
"                          ->  Seq Scan on romaneios r2  (cost=0.00..1621.23 
rows=18123 width=20)"
"              ->  Hash  (cost=1621.23..1621.23 rows=18123 width=20)"
"                    ->  Seq Scan on romaneios r1  (cost=0.00..1621.23 
rows=18123 width=20)"
"        ->  Hash  (cost=1.02..1.02 rows=2 width=6)"
"              ->  Seq Scan on nfe_contingencia ctg  (cost=0.00..1.02 rows=2 
width=6)"
_______________________________________________
pgbr-geral mailing list
pgbr-geral@listas.postgresql.org.br
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a