DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: problematic queries

  1. #1
    Join Date
    Oct 2000
    Posts
    139

    problematic queries

    Hi

    I have a query which takes over 10 hours to finish so I rewrote the query twice. The problem is different number of rows are returned, I dont see why because to me the logic of all three queries are the same!

    Any thoughts?

    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;
    Last edited by Sweetie; 10-01-2003 at 03:32 PM.

  2. #2
    Join Date
    Oct 2000
    Posts
    139
    any suggestions...?

  3. #3
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Could you post the number of rows in each table?

    tamil

  4. #4
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Based on the first query, I rewrote it.
    Try this:
    select count(1)
    from (select /*+ no_merge */
    cod_empresa,
    id_cliente,
    id_cliente ,
    cod_contrato,
    cod_contrato_detalle,
    max(num_version_detalle) as max_num_version_detalle
    from contrato_detalle_lineas
    where dom64_operacion_realizada != 'SIN'
    and fch_registro is not null
    group by cod_empresa,
    id_cliente,
    id_cliente ,
    cod_contrato,
    cod_contrato_detalle
    ) E,
    ( select /*+ no_merge */
    cod_empresa,
    id_cliente,
    id_cliente ,
    cod_contrato,
    cod_contrato_detalle,
    from contrato_detalle_lineas
    where cod_empresa = 'TSAI'
    and dom64_operacion_realizada != 'BAJ'
    and estado_atis is null
    ) cont_det,
    ( select /*+ no_merge */
    COD_EMPRESA, ID_CLIENTE, COD_CONTRATO
    from contratos
    where COD_SERVICIO = 'IMAIL'
    ) cont,
    clientes cli
    where 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_det.cod_empresa = e.cod_empresa
    and cont_det.id_cliente = e.id_cliente
    and cont_det.cod_contrato = e.cod_contrato
    and cont_det.cod_contrato_detalle = e.cod_contrato_detalle
    and cont_det.num_version_detalle = e.max_num_version_detalle ;

    Tamil

  5. #5
    Join Date
    Oct 2000
    Posts
    139
    Hi

    the tables have following number of rows

    Code:
    TABLE_NAME                       NUM_ROWS     BLOCKS EMPTY_BLOCKS
    ------------------------------ ---------- ---------- ------------
    CLIENTES                            57294       1535            0
    CONTRATOS                           64009       2363           68
    CONTRATO_DETALLE_LINEAS           2462198      53502          129
    The problem is not the query speed, the problem is all three queries returns different number of count(*)! I dont see why!

    Cheers

  6. #6
    Join Date
    May 2000
    Location
    ATLANTA, GA, USA
    Posts
    3,135
    Instead of count(*), select columns from relevant tables by giving a value for the column, COD_EMPRESA. And check manually.

    Tamil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width