
                                                                     QUERY PLAN

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=7971.65..7971.68 rows=1 width=833) (actual time=1501529.16..1508066.32 rows=1755 loops=1)
   ->  Group  (cost=7971.65..7971.66 rows=1 width=833) (actual time=1501495.07..1502517.99 rows=24111 loops=1)
         ->  Sort  (cost=7971.65..7971.65 rows=1 width=833) (actual time=1501495.03..1501563.87 rows=24111 loops=1)
               Sort Key: ftnfco00.estado_cliente, ftcofi00.grupo_faturamento, ((((ftprod00.tipo_cadastro)::text || (ftprod00.codigo_produto)::text) || '||'::text) || (gsames00.ano_mes)::text), ((ftprod00.descricao_produto || '||'::character varying) || gsames00.descricao)
               ->  Nested Loop  (cost=7913.31..7971.64 rows=1 width=833) (actual time=1382281.24..1495748.79 rows=24111loops=1)
                     Join Filter: ("inner".estado_sigla = "outer".estado_cliente)
                     ->  Nested Loop  (cost=7913.31..7969.90 rows=1 width=827) (actual time=1382271.41..1490702.11 rows=24111 loops=1)
                           Join Filter: (("outer".emp = "inner".emp) AND ("inner".fil = "outer".empfil) AND ("inner".codigo = "outer".bandeira_cliente))
                           ->  Nested Loop  (cost=7913.31..7968.87 rows=1 width=795) (actual time=1382179.80..1486492.69 rows=24111 loops=1)
                                 Join Filter: (("inner".emp = "outer".emp) AND ("outer".codigo = "inner".cla_categoria)AND ("outer".codigo = "inner".cla_marca) AND ("outer".codigo = "inner".situacao))
                                 ->  Nested Loop  (cost=7913.31..7954.34 rows=3 width=705) (actual time=1381632.57..1464651.32 rows=241110 loops=1)
                                       Join Filter: (("outer".emp = "inner".emp) AND ("inner".fil = "outer".empfil))                                       ->  Nested Loop  (cost=7913.31..7941.84 rows=2 width=665) (actual time=1381444.87..1408531.29 rows=241110 loops=1)
                                             Join Filter: (("inner".emp = "outer".emp) AND ("outer".fil = "inner".empfil) AND("outer".codigo_grupo_rep = "inner".codigo_grupo_rep))
                                             ->  Seq Scan on ftgrep00  (cost=0.00..1.01 rows=1 width=32) (actual time=28.97..28.97 rows=1 loops=1)
                                             ->  Materialize  (cost=7940.79..7940.79 rows=2 width=633) (actual time=1381415.51..1385445.39 rows=241110 loops=1)
                                                   ->  Nested Loop  (cost=7913.31..7940.79 rows=2 width=633) (actual time=1348400.96..1379090.93 rows=241110 loops=1)
                                                         Join Filter: (("inner".emp = "outer".emp) AND ("outer".fil = "inner".empfil) AND ("outer".codigo_supervisor = "inner".codigo_supervisor))
                                                         ->  Seq Scan on ftsupv00  (cost=0.00..1.01 rows=1 width=32) (actual time=13.92..13.94 rows=1 loops=1)
                                                         ->  Materialize  (cost=7939.75..7939.75 rows=2 width=601) (actual time=1348386.88..1357853.77 rows=241110 loops=1)
                                                               ->  Nested Loop  (cost=7913.31..7939.75 rows=2 width=601) (actual time=238548.84..1344339.33 rows=241110 loops=1)
                                                                     Join Filter: (("inner".ano_mes)::text = to_char("outer".data_emissao, 'YYYYMM'::text))
                                                                     ->  Merge Join  (cost=7913.31..7913.45 rows=2 width=576)(actual time=238524.31..275724.44 rows=241110 loops=1)
                                                                           Merge Cond: (("outer".empfil = "inner".fil) AND ("outer".codigo_tipo_cliente = "inner".cod_tipocliente) AND ("outer".emp = "inner".emp))
                                                                           ->  Sort  (cost=7911.79..7911.80 rows=2 width=543)(actual time=238523.48..254284.13 rows=241110 loops=1)
                                                                                 Sort Key: ftclcr00.fil, ftclcr00.codigo_tipo_cliente, ftnfpr00.emp
                                                                                 ->  Nested Loop  (cost=7851.29..7911.78 rows=2 width=543) (actual time=80005.55..144755.84 rows=241110 loops=1)
                                                                                       Join Filter: (("inner".emp_estado = "outer".estado_cliente) AND ("outer".codigo = "inner".codigo_grupo_cliente))
                                                                                       ->  Nested Loop  (cost=7851.29..7853.22 rows=10 width=449) (actual time=79960.48..94206.17 rows=241110 loops=1)
                                                                                             Join Filter: (("inner".emp = "outer".emp) AND ("outer".fil = "inner".empfil))
                                                                                             ->  Seq Scan on ftclgr00  (cost=0.00..1.01 rows=1 width=32) (actual time=12.21..12.21 rows=1 loops=1)                                                                                             
->  Materialize  (cost=7852.06..7852.06 rows=10 width=417) (actual time=79948.16..82051.01 rows=241110 loops=1)
                                                                                                   
->  Merge Join  (cost=7851.29..7852.06 rows=10 width=417) (actual time=65536.22..78515.30 rows=241110 loops=1)
                                                                                                       
Merge Cond: (("outer".empfil = "inner".fil) AND ("outer".cod_repres = "inner".codigo_repr) AND ("outer".emp = "inner".emp))
                                                                                                         
->  Sort  (cost=7840.51..7840.54 rows=10 width=364) (actual time=64203.33..65522.80 rows=241110 loops=1)
                                                                                                               
Sort Key: ftnfco00.empfil, ftnfco00.cod_repres, ftnfpr00.emp
                                                                                                               
->  Nested Loop
  (cost=7569.51..7840.35 rows=10 width=364) (actual time=2094.38..31469.51 rows=241110 loops=1)
                                                                                                                     
Join Filter: (("outer".emp = "inner".emp) AND ("outer".empfil = "inner".fil))
                                                                                                                     
->  Nested Loop  (cost=7569.51..7839.10 rows=1 width=337) (actual time=2094.00..21568.08 rows=24111 loops=1)                                                                                                                     
Join Filter: (("outer".emp = "inner".emp) AND ("outer".empfil = "inner".fil))
                                                                                                                          
-> Nested Loop  (cost=7569.51..7838.07 rows=1 width=305) (actual time=2086.59..20456.24 rows=24111 loops=1)
   Join Filter: (("outer".emp = "inner".emp) AND ("outer".empfil = "inner".fil))
   ->  Nested Loop  (cost=7569.51..7837.05 rows=1 width=273) (actual time=2078.21..19368.42 rows=24111 loops=1)
         ->  Merge Join  (cost=7569.51..7572.07 rows=44 width=161) (actual time=1991.00..2262.54 rows=7718 loops=1)
               Merge Cond: (("outer".cod_fiscal = "inner".codigo_fiscal) AND ("outer".empfil = "inner".fil) AND ("outer".emp = "inner".emp))
               ->  Sort  (cost=7520.71..7521.45 rows=295 width=109) (actual time=1958.40..1994.38 rows=9339 loops=1)
                     Sort Key: ftnfco00.cod_fiscal, ftnfco00.empfil, ftnfco00.emp
                     ->  Seq Scan on ftnfco00  (cost=0.00..7508.60 rows=295 width=109) (actual time=14.50..1581.81 rows=9339 loops=1)
                           Filter: ((emp = 909::numeric) AND (situacao_nf = 'N'::character varying) AND (to_char(data_emissao, 'YYYYMM'::text) >= '200209'::text) AND (to_char(data_emissao, 'YYYYMM'::text) <= '200304'::text))
               ->  Sort  (cost=48.80..48.98 rows=72 width=52) (actual time=31.86..43.69 rows=7765 loops=1)
                     Sort Key: ftcofi00.codigo_fiscal, ftcofi00.fil, ftcofi00.emp
                     ->  Seq Scan on ftcofi00  (cost=0.00..46.58 rows=72 width=52) (actual time=7.62..31.07 rows=75 loops=1)
                           Filter: ((grupo_faturamento >= '01'::character varying) AND ((atual_fatura = '+'::character varying) OR (atual_fatura = '-'::character varying) OR (nf_prodgratis = 'S'::character varying)))
         ->  Index Scan using ftnfpr00_pkey on ftnfpr00  (cost=0.00..5.95 rows=1 width=112) (actual time=1.41..2.14 rows=3 loops=7718)
               Index Cond: (("outer".emp = ftnfpr00.emp) AND ("outer".fil = ftnfpr00.fil) AND (ftnfpr00.fil = 101::numeric) AND ("outer".nota_fiscal = ftnfpr00.nota_fiscal) AND ("outer".serie = ftnfpr00.serie) AND ("outer".data_emissao = ftnfpr00.data_emissao))
   ->  Seq Scan on ftcgca00  (cost=0.00..1.01 rows=1 width=32) (actual time=0.01..0.01 rows=1 loops=24111)                                                                                                                           
-> Seq Scan on ftcgma00  (cost=0.00..1.01 rows=1 width=32) (actual time=0.01..0.01 rows=1 loops=24111)                                                                                                                     
->  Seq Scan on ftspro00  (cost=0.00..1.10 rows=10 width=27) (actual time=0.00..0.04 rows=10 loops=24111)                                                                                                         
->  Sort  (cost=10.78..11.05 rows=109 width=53) (actual time=1332.54..1510.69 rows=241175 loops=1)                                                                                                               
Sort Key: ftrepr00.fil, ftrepr00.codigo_repr, ftrepr00.emp                                                                                                               
->  Seq Scan on ftrepr00  (cost=0.00..7.09 rows=109 width=53) (actual time=406.63..1331.33 rows=109 loops=1)                                                                                       
->  Index Scan using ftclcr01 on ftclcr00  (cost=0.00..5.83 rows=1 width=94) (actual time=0.13..0.14 rows=1 loops=241110)                                                                                             
Index Cond: ((ftclcr00.emp = "outer".emp) AND (ftclcr00.fil = "outer".empfil) AND (ftclcr00.tipo_cadastro = "outer".tipo_cad_clicre) AND (ftclcr00.codigo = "ou
ter".cod_cliente))
                                                                           ->  Sort  (cost=1.52..1.56 rows=17 width=33) (actual time=0.61..225.43 rows=240986 loops=1)                                                                                 
Sort Key: fttcli00.fil, fttcli00.cod_tipocliente, fttcli00.emp                                                                                 
->  Seq Scan on fttcli00  (cost=0.00..1.17 rows=17 width=33) (actual time=0.12..0.17 rows=17 loops=1)
                                                                     ->  Seq Scan on gsames00  (cost=0.00..6.72 rows=372 width=25) (actual time=0.00..1.28 rows=372 loops=241110)
                                       ->  Index Scan using ftredc00_pkey on ftredc00  (cost=0.00..5.83 rows=1 width=40) (actual time=0.12..0.12 rows=1 loops=241110)
                                             Index Cond: ((ftredc00.emp = "outer".emp) AND (ftredc00.fil = "outer".fil) AND (f
tredc00.tipo_contribuinte = "outer".tipo_contribuinte) AND (ftredc00.codigo_rede = "outer".codigo_rede))
                                 ->  Index Scan using ftprod00_pkey on ftprod00  (cost=0.00..5.74 rows=1 width=90) (actual time=0.06..0.06 rows=1 loops=241110)
                                       Index Cond: ((ftprod00.emp = "outer".emp) AND (ftprod00.fil = "outer".empfil) AND (ftprod00.tipo_cadastro = "outer".tipo_cad_promat) AND (ftprod00.codigo_produto = "outer".cod_produto))
                           ->  Seq Scan on ftband00  (cost=0.00..1.01 rows=1 width=32) (actual time=0.01..0.01 rows=1 loops=24111)
                     ->  Seq Scan on gsesta00  (cost=0.00..1.33 rows=33 width=6) (actual time=0.01..0.08 rows=33 loops=24111) Total runtime: 1508259.91 msec
(74 rows)
