Bom dia pessoal, nao sei se posso postar este tipo de topico na lista, mas
ca estou eu.
Sou bem iniciando no mundo SQL, e preciso otimizar esta consulta.

select c_cpd, sum(n_cant) from (
    select c_cpd, sum(n_cant) as n_cant from movie
        where c_sucursal>='001' and c_sucursal<='001'
        group by c_cpd
    union all
    select c_cpd, sum(n_cant)*-1 as n_cant from movis
        where c_sucursal>='001' and c_sucursal<='001'
        group by c_cpd
) as movi
group by c_cpd
order by c_cpd


Sort  (cost=6785.20..6785.70 rows=200 width=41)
  Sort Key: movie.c_cpd
  ->  HashAggregate  (cost=6775.06..6777.56 rows=200 width=41)
        ->  Append  (cost=1156.94..6734.38 rows=8136 width=41)
              ->  HashAggregate  (cost=1156.94..1230.74 rows=5904 width=18)
                    ->  Seq Scan on movie  (cost=0.00..1029.95 rows=25397
width=18)
                          Filter: ((c_sucursal >= '001'::bpchar) AND
(c_sucursal <= '001'::bpchar))
              ->  HashAggregate  (cost=5388.80..5422.28 rows=2232 width=18)
                    ->  Seq Scan on movis  (cost=0.00..4883.60 rows=101040
width=18)
                          Filter: ((c_sucursal >= '001'::bpchar) AND
(c_sucursal <= '001'::bpchar))



Grato por qualquer ajuda, e/ou comentarios.

-- 
*:-.,_,.-:*'``'*:-.,_,.-:*:-.,_,.-:*'``'*:-.,_,.-:
       :: Rodrigo Machado ::
           FlaRo Sistemas

         Linux User #338262
Lat S25º49'20.11' - Lon W55º07'12.05''
*:-.,_,.-:*'``'*:-.,_,.-:*:-.,_,.-:*'``'*:-.,_,.-:
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a