Code:
FIRST QUERY, over 10 hours
=============================
select /*+ use_hash(cont_det, cli, cont, e) */ count(*)
from tgcfit10.contrato_detalle_lineas cont_det,
tgcfit10.clientes cli,
tgcfit10.contratos cont
where cont_det.estado_atis is null
and cont_det.COD_EMPRESA = cli.COD_EMPRESA
and cont_det.ID_CLIENTE = cli.ID_CLIENTE
and cont_det.cod_empresa = cont.COD_EMPRESA
and cont_det.ID_CLIENTE = cont.ID_CLIENTE
and cont_det.COD_CONTRATO = cont.COD_CONTRATO
and CONT.COD_SERVICIO = 'IMAIL'
and cont_det.cod_empresa = 'TSAI'
and cont_det.num_version_detalle = (select max(num_version_detalle)
from tgcfit10.contrato_detalle_lineas e
where e.cod_empresa = cont_det.cod_empresa
and e.id_cliente = cont_det.id_cliente
and e.cod_contrato = cont_det.cod_contrato
and e.cod_contrato_detalle = cont_det.cod_contrato_detalle
and e.dom64_operacion_realizada != 'SIN'
and e.fch_registro is not null)
and cont_det.dom64_operacion_realizada != 'BAJ';
SECOND QUERY 2 minutes
=============================
select /*+ use_hash(x, z) */ count(*)
from (select /*+ use_hash(cont_det, cli, cont) */
cli.id_fiscal,
cont_det.num_version_detalle,
cont_det.cod_empresa,
cont_det.id_cliente,
cont_det.cod_contrato,
cont_det.cod_contrato_detalle,
cont_det.dom64_operacion_realizada,
cont_det.fch_registro
from tgcfit10.contrato_detalle_lineas cont_det,
tgcfit10.clientes cli,
tgcfit10.contratos cont
where cont_det.estado_atis is null
and cont_det.COD_EMPRESA = cli.COD_EMPRESA
and cont_det.ID_CLIENTE = cli.ID_CLIENTE
and cont_det.cod_empresa = cont.COD_EMPRESA
and cont_det.ID_CLIENTE = cont.ID_CLIENTE
and cont_det.COD_CONTRATO = cont.COD_CONTRATO
and cont.cod_servicio = 'IMAIL'
and cont_det.cod_empresa = 'TSAI'
and cont_det.dom64_operacion_realizada != 'BAJ') x,
(select y.cod_empresa, y.id_cliente, y.cod_contrato, y.cod_contrato_detalle,
max(y.num_version_detalle)
over (partition by y.cod_empresa, y.id_cliente,
y.cod_contrato, y.cod_contrato_detalle) maxversion
from tgcfit10.contrato_detalle_lineas Y
where y.fch_registro is not null
and y.dom64_operacion_realizada != 'SIN') z
where x.num_version_detalle = z.maxversion
and x.cod_empresa = z.cod_empresa
and x.id_cliente = z.id_cliente
and x.cod_contrato = z.cod_contrato
and x.cod_contrato_detalle = z.cod_contrato_detalle;
THRID QUERY, two minutes
=============================
select count(*)
from (select /*+ use_hash(cont_det, cli, cont) */
cli.id_fiscal,
num_version_detalle,
cont_det.cod_empresa,
cont_det.id_cliente,
cont_det.cod_contrato,
cont_det.cod_contrato_detalle,
cont_det.dom64_operacion_realizada,
cont_det.fch_registro,
max(case when fch_registro is not null and dom64_operacion_realizada != 'SIN'
then num_version_detalle
else null
end)
over (partition by cont_det.cod_empresa, cont_det.id_cliente,
cont_det.cod_contrato, cont_det.cod_contrato_detalle) maxversion
from tgcfit10.contrato_detalle_lineas cont_det,
tgcfit10.clientes cli,
tgcfit10.contratos cont
where cont_det.estado_atis is null
and cont_det.COD_EMPRESA = cli.COD_EMPRESA
and cont_det.ID_CLIENTE = cli.ID_CLIENTE
and cont_det.cod_empresa = cont.COD_EMPRESA
and cont_det.ID_CLIENTE = cont.ID_CLIENTE
and cont_det.COD_CONTRATO = cont.COD_CONTRATO
and cont.cod_servicio = 'IMAIL'
and cont_det.cod_empresa = 'TSAI'
and cont_det.dom64_operacion_realizada != 'BAJ') x
where num_version_detalle = maxversion;