Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/12/2011, 10:20
massu_vago
 
Fecha de Ingreso: diciembre-2008
Mensajes: 88
Antigüedad: 15 años, 4 meses
Puntos: 0
Pregunta Optimizar consulta

saludos amigos de foros del web.

nuevamente requiero de su ayuda, en esta ocacion para optimizar una consulta que demora alrededor de 15 minutos es ejecutarce y entenderan que no es un tiempo aceptable de espera para el usuario.

lo que hace mi consulta es generar una especie de pivot table(tabla horizontal) con ciertos datos filtrados por fecha y devuelve algo asi:
|DIA 1|DIA 2|DIA3|DIA4|DIA5| DIA6| Dia7 |
ciclo 1 | 234 | 520 | 534 | 800 | 923 | 234 | 298 |
ciclo 2 | 234 | 520 | 534 | 800 | 923 | 234 | 298 |
ciclo 3 | 234 | 520 | 534 | 800 | 923 | 234 | 298 |
ciclo 4 | 234 | 520 | 534 | 800 | 923 | 234 | 298 |
ciclo 5 | 234 | 520 | 534 | 800 | 923 | 234 | 298 |

y la consulta es

Código MySQL:
Ver original
  1. select t2.NMRO_CICLO,
  2.     CASE    
  3.         WHEN  1=1
  4.             THEN
  5.             (select sum(t3.cant_cuentas)
  6.     from (
  7.     select NMRO_CICLO,
  8.     count(distinct NMRO_CUENTA) as cant_cuentas
  9.     from ces_ivr_reporte_comercial, ces_ivr_eventos  
  10.     where RUT_CUENTA=RUT and ABREV_EVENTO='comercial'
  11.     and FECHA = (select DATE_SUB(max(FECHA),INTERVAL 6 DAY) from ces_ivr_eventos)
  12.     group by RUT_CUENTA
  13.     having count(distinct NMRO_CUENTA) < 2) t3
  14.     where NMRO_CICLO=t2.NMRO_CICLO
  15.     group by NMRO_CICLO)
  16.             ELSE 0 END AS DIA_1,
  17.     CASE
  18.         WHEN 1=1
  19.             THEN
  20.             (select sum(t3.cant_cuentas)
  21.     from (
  22.     select NMRO_CICLO,
  23.     count(distinct NMRO_CUENTA) as cant_cuentas
  24.     from ces_ivr_reporte_comercial, ces_ivr_eventos  
  25.     where RUT_CUENTA=RUT and ABREV_EVENTO='comercial'
  26.     and FECHA = (select DATE_SUB(max(FECHA),INTERVAL 5 DAY) from ces_ivr_eventos)
  27.     group by RUT_CUENTA
  28.     having count(distinct NMRO_CUENTA) < 2) t3
  29.     where NMRO_CICLO=t2.NMRO_CICLO
  30.     group by NMRO_CICLO)
  31.             ELSE 0 END AS DIA_2,
  32.     CASE
  33.         WHEN 1=1
  34.             THEN (select sum(t3.cant_cuentas)
  35.     from (
  36.     select NMRO_CICLO,
  37.     count(distinct NMRO_CUENTA) as cant_cuentas
  38.     from ces_ivr_reporte_comercial, ces_ivr_eventos  
  39.     where RUT_CUENTA=RUT and ABREV_EVENTO='comercial'
  40.     and FECHA = (select DATE_SUB(max(FECHA),INTERVAL 4 DAY) from ces_ivr_eventos)
  41.     group by RUT_CUENTA
  42.     having count(distinct NMRO_CUENTA) < 2) t3
  43.     where NMRO_CICLO=t2.NMRO_CICLO
  44.     group by NMRO_CICLO)
  45.             ELSE 0 END AS DIA_3,
  46.     CASE
  47.         WHEN 1=1
  48.             THEN (select sum(t3.cant_cuentas)
  49.     from (
  50.     select NMRO_CICLO,
  51.     count(distinct NMRO_CUENTA) as cant_cuentas
  52.     from ces_ivr_reporte_comercial, ces_ivr_eventos  
  53.     where RUT_CUENTA=RUT and ABREV_EVENTO='comercial'
  54.     and FECHA = (select DATE_SUB(max(FECHA),INTERVAL 3 DAY) from ces_ivr_eventos)
  55.     group by RUT_CUENTA
  56.     having count(distinct NMRO_CUENTA) < 2) t3
  57.     where NMRO_CICLO=t2.NMRO_CICLO
  58.     group by NMRO_CICLO)
  59.             ELSE 0 END AS DIA_4,
  60.     CASE
  61.         WHEN 1=1
  62.             THEN (select sum(t3.cant_cuentas)
  63.     from (
  64.     select NMRO_CICLO,
  65.     count(distinct NMRO_CUENTA) as cant_cuentas
  66.     from ces_ivr_reporte_comercial, ces_ivr_eventos  
  67.     where RUT_CUENTA=RUT and ABREV_EVENTO='comercial'
  68.     and FECHA = (select DATE_SUB(max(FECHA),INTERVAL 2 DAY) from ces_ivr_eventos)
  69.     group by RUT_CUENTA
  70.     having count(distinct NMRO_CUENTA) < 2) t3
  71.     where NMRO_CICLO=t2.NMRO_CICLO
  72.     group by NMRO_CICLO)
  73.             ELSE 0 END AS DIA_5,
  74.     CASE
  75.         WHEN 1=1
  76.             THEN (select sum(t3.cant_cuentas)
  77.     from (
  78.     select NMRO_CICLO,
  79.     count(distinct NMRO_CUENTA) as cant_cuentas
  80.     from ces_ivr_reporte_comercial, ces_ivr_eventos  
  81.     where RUT_CUENTA=RUT and ABREV_EVENTO='comercial'
  82.     and FECHA = (select DATE_SUB(max(FECHA),INTERVAL 1 DAY) from ces_ivr_eventos)
  83.     group by RUT_CUENTA
  84.     having count(distinct NMRO_CUENTA) < 2) t3
  85.     where NMRO_CICLO=t2.NMRO_CICLO
  86.     group by NMRO_CICLO)
  87.             ELSE 0 END AS DIA_6,
  88.     CASE
  89.         WHEN 1=1
  90.             THEN (select sum(t3.cant_cuentas)
  91.     from (
  92.     select NMRO_CICLO,
  93.     count(distinct NMRO_CUENTA) as cant_cuentas
  94.     from ces_ivr_reporte_comercial, ces_ivr_eventos  
  95.     where RUT_CUENTA=RUT and ABREV_EVENTO='comercial'
  96.     and FECHA = (select DATE_SUB(max(FECHA),INTERVAL 0 DAY) from ces_ivr_eventos)
  97.     group by RUT_CUENTA
  98.     having count(distinct NMRO_CUENTA) < 2) t3
  99.     where NMRO_CICLO=t2.NMRO_CICLO
  100.     group by NMRO_CICLO)
  101.             ELSE 0 END AS DIA_7
  102.     from (
  103.     select NMRO_CICLO,NMRO_CUENTA,RUT_CUENTA,
  104.     count(distinct NMRO_CUENTA)   as cant_cuentas, FECHA
  105.     from ces_ivr_reporte_comercial
  106.     inner join ces_ivr_eventos
  107.     on RUT_CUENTA=RUT
  108.     where ABREV_EVENTO='comercial'
  109.     and FECHA between (select DATE_SUB(max(FECHA),INTERVAL 7 DAY)) and (select max(FECHA))
  110.     group by RUT_CUENTA
  111.     having count(distinct NMRO_CUENTA) < 2) t2
  112.     group by t2.NMRO_CICLO;

nose si me pueden ayudar a optimizarla para que se reduzca el tiempo de ejecucion de la consulta. de antemano muchas gracias.