Amigos los molesto para pedirles si me pueden ayudar a optimizar la siguiente consulta:
SELECT uploaddet_importcomp.indice, historicotemp.clavebeneficiario, historicotemp.activo FROM uploaddet_importcomp left join nacer.historicotemp on uploaddet_importcomp.fil_clasedoc = historicotemp.aficlasedoc AND uploaddet_importcomp.fil_tipodoc = historicotemp.afitipodoc AND uploaddet_importcomp.fil_nrodoc = historicotemp.afidni AND uploaddet_importcomp.fil_nacim::date = historicotemp.afifechanac::date WHERE uploaddet_importcomp.estado='S' and date_trunc('month', uploaddet_importcomp.pres_fecha::date) = historicotemp.periodo El explain me dice: "Merge Join (cost=2986610.29..3321028.99 rows=1 width=22)" " Merge Cond: (((historicotemp.afidni)::text = uploaddet_importcomp.fil_nrodoc) AND (((historicotemp.aficlasedoc)::text) = uploaddet_importcomp.fil_clasedoc) AND ((historicotemp.afitipodoc)::text = uploaddet_importcomp.fil_tipodoc) AND (((historicotemp.afi (...)" " -> Sort (cost=2933436.44..2957517.55 rows=9632444 width=47)" " Sort Key: historicotemp.afidni, ((historicotemp.aficlasedoc)::text), historicotemp.afitipodoc, ((historicotemp.afifechanac)::date), historicotemp.periodo" " -> Seq Scan on historicotemp (cost=0.00..630844.44 rows=9632444 width=47)" " -> Materialize (cost=49759.11..50258.96 rows=99970 width=40)" " -> Sort (cost=49759.11..50009.04 rows=99970 width=40)" " Sort Key: uploaddet_importcomp.fil_nrodoc, uploaddet_importcomp.fil_clasedoc, uploaddet_importcomp.fil_tipodoc, ((uploaddet_importcomp.fil_nacim)::date), (date_trunc('month'::text, ((uploaddet_importcomp.pres_fecha)::date)::timestamp with tim (...)" " -> Seq Scan on uploaddet_importcomp (cost=0.00..38720.00 rows=99970 width=40)" " Filter: (estado = 'S'::text)" Tengo índices por uploaddet_importcomp.estado historicotemp.dni Puedo crear mas índice si es necesario, la historicotemp (que es muy grande) se actualiza solo una vez al mes Desde ya agradezco la orientación que me puedan dar. Un abrazo