Bom dia pessoal, perdoem minha ignorância, mas alguém poderia me explicar
porque as duas consultas abaixo tem tempos de execução tão diferentes?

EXPLAIN ANALYZE
SELECT MAX( dh_emissao )
FROM nf_venda
WHERE dh_movimentacao_caixa >= ( getdate()::date - 2 )

Aggregate  (cost=98997.00..98997.01 rows=1 width=8) (actual
time=2268.764..2268.764 rows=1 loops=1)
  ->  Seq Scan on nf_venda  (cost=0.00..98716.50 rows=112200 width=8)
(actual time=608.876..2268.601 rows=680 loops=1)
        Filter: (dh_movimentacao_caixa >= ((getdate())::date - 2))
        Rows Removed by Filter: 343186
Planning time: 0.289 ms
Execution time: 2268.817 ms

__________________________________________________________

EXPLAIN ANALYZE
SELECT MAX( dh_emissao )
FROM nf_venda
WHERE dh_movimentacao_caixa >= ( SELECT getdate()::date - 2 )

Aggregate  (cost=12505.24..12505.25 rows=1 width=8) (actual
time=0.704..0.704 rows=1 loops=1)
  InitPlan 1 (returns $0)
    ->  Result  (cost=0.00..0.27 rows=1 width=0) (actual time=0.072..0.072
rows=1 loops=1)
  ->  Bitmap Heap Scan on nf_venda  (cost=2145.97..12224.47 rows=112200
width=8) (actual time=0.223..0.465 rows=681 loops=1)
        Recheck Cond: (dh_movimentacao_caixa >= $0)
        Heap Blocks: exact=41
        ->  Bitmap Index Scan on idx_dh_movimentacao  (cost=0.00..2117.92
rows=112200 width=0) (actual time=0.206..0.206 rows=681 loops=1)
              Index Cond: (dh_movimentacao_caixa >= $0)
Planning time: 0.396 ms
Execution time: 0.796 ms
__________________________________________________________

Detalhes:
 - Função getdate( ) existe para compatibilidade entre SGDB's

CREATE OR REPLACE FUNCTION getdate() RETURNS timestamp without time zone AS
$BODY$
BEGIN
RETURN clock_timestamp();
END;
$BODY$
  LANGUAGE plpgsql VOLATILE COST 100;

 - Índice sobre o campo dh_movimentacao_caixa
   CREATE INDEX idx_dh_movimentacao  ON nf_venda  USING btree
 (dh_movimentacao_caixa)

 PostgreSQL 9.4.1, compiled by Visual C++ build 1800, 32-bit (Windows)
__________________________________________________________

Agradeço antecipadamente os esclarecimentos, e gostaria de entender melhor
cada detalhe do resultado do EXPLAIN ANALYZE, onde eu encontro documentação
detalhada sobre isso?

Grato,

Fernando Luís Cambiaghi
*[email protected] <[email protected]>*
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a