OPTIMIZACION Y SEGUIMIENTO HOJA DE RUTA!!!!
La primera consulta optimizada tomando el año mas antiguo:
Se quitó el comodín *(asterisco) y se dejaron solo los campos que se necesitan en la consulta.
Se utilizó la instrucción sql "between" en las fechas para reemplazar los operadores lógicos ">=" "<=".
Ojo!!!! Revisar índices, puede ayudar a optimizar mas.
explain analyze select nov_id, nov_tipo, nov_subtipo, nov_consecutivo, usuario_nombres, cliente_nombre, nov_fecha_entrega_prod, nov_fecha_fin_pauta, nov_fecha_ini_pauta FROM novedades, usuarios, clientes where novedades.usuario_id=usuarios.usuario_id and novedades.cliente_id=clientes.cliente_id and nov_estado=1 and nov_aprobacion=1 and nov_anulada<>1 and nov_subtipo<>4 and nov_subtipo<>8 and nov_fecha between '2006-07-30' and '2008-07-30';
- Hash Join (cost=47.51..984.77 rows=4387 width=164) (actual time=2.988..27.941 rows=1267 loops=1)
- Hash Cond: ("outer".usuario_id = "inner".usuario_id)
- -> Hash Join (cost=42.71..914.16 rows=4387 width=100) (actual time=2.583..24.983 rows=1267 loops=1)
- Hash Cond: ("outer".cliente_id = "inner".cliente_id)
- -> Seq Scan on novedades (cost=0.00..805.65 rows=4387 width=36) (actual time=0.490..20.353 rows=1267 loops=1)
- Filter: ((nov_estado = 1) AND (nov_aprobacion = 1) AND (nov_anulada <> 1) AND (nov_subtipo <> 4) AND (nov_subtipo <> 8) AND (nov_fecha >= '2006-07-30'::date) AND (nov_fecha <= '2008-07-30'::date))
- -> Hash (cost=40.17..40.17 rows=1017 width=72) (actual time=2.075..2.075 rows=656 loops=1)
- -> Seq Scan on clientes (cost=0.00..40.17 rows=1017 width=72) (actual time=0.021..1.190 rows=656 loops=1)
- -> Hash (cost=4.44..4.44 rows=144 width=72) (actual time=0.243..0.243 rows=66 loops=1)
- -> Seq Scan on usuarios (cost=0.00..4.44 rows=144 width=72) (actual time=0.011..0.141 rows=66 loops=1)
- Total runtime: 29.007 ms
#########################################################################################################
(PAUTA):
Primera consulta: Para traer todo el detalle de pauta
Original:
explain analyze select * from detalle_pauta,aforo,productos,locaciones,ciudades,elementos,elements where detalle_pauta.aforo_id=aforo.aforo_id and aforo.loc_id=locaciones.loc_id and aforo.producto_id=productos.producto_id and productos.producto_id=locaciones.producto_id and locaciones.ciudad_id=ciudades.ciudad_id and aforo.ele_id=elements.ele_id and elementos.elemento_id=elements.elemento_id and detalle_pauta.nov_id=2682
- Nested Loop (cost=45.72..516.94 rows=1 width=560) (actual time=8.346..10.743 rows=13 loops=1)
- Join Filter: ("inner".elemento_id = "outer".elemento_id)
- -> Nested Loop (cost=45.72..514.54 rows=1 width=494) (actual time=8.249..8.716 rows=13 loops=1)
- -> Hash Join (cost=45.72..508.70 rows=1 width=436) (actual time=8.173..8.457 rows=13 loops=1)
- Hash Cond: (("outer".loc_id = "inner".loc_id) AND ("outer".producto_id = "inner".producto_id))
- -> Nested Loop (cost=0.00..462.42 rows=54 width=203) (actual time=0.141..0.388 rows=13 loops=1)
- -> Index Scan using novedad_detalle_pauta on detalle_pauta (cost=0.00..138.06 rows=54 width=124) (actual time=0.076..0.122 rows=13 loops=1)
- Index Cond: (nov_id = 2682)
- -> Index Scan using aforo_pkey on aforo (cost=0.00..5.99 rows=1 width=79) (actual time=0.014..0.015 rows=1 loops=13)
- Index Cond: ("outer".aforo_id = aforo.aforo_id)
- -> Hash (cost=45.58..45.58 rows=28 width=233) (actual time=8.011..8.011 rows=1059 loops=1)
- -> Hash Join (cost=2.89..45.58 rows=28 width=233) (actual time=0.280..6.079 rows=1059 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=1.16..43.10 rows=95 width=171) (actual time=0.102..3.720 rows=1059 loops=1)
- Hash Cond: ("outer".producto_id = "inner".producto_id)
- -> Seq Scan on locaciones (cost=0.00..33.66 rows=1466 width=148) (actual time=0.023..1.393 rows=1059 loops=1)
- -> Hash (cost=1.13..1.13 rows=13 width=23) (actual time=0.062..0.062 rows=13 loops=1)
- -> Seq Scan on productos (cost=0.00..1.13 rows=13 width=23) (actual time=0.020..0.034 rows=13 loops=1)
- -> Hash (cost=1.58..1.58 rows=58 width=62) (actual time=0.162..0.162 rows=65 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.019..0.083 rows=65 loops=1)
- -> Index Scan using elements_pkey on elements (cost=0.00..5.83 rows=1 width=58) (actual time=0.014..0.015 rows=1 loops=13)
- Index Cond: ("outer".ele_id = elements.ele_id)
- -> Seq Scan on elementos (cost=0.00..1.62 rows=62 width=66) (actual time=0.003..0.083 rows=78 loops=13)
- Total runtime: 11.170 ms
Optimizada:
Se quitó el comodín *(asterisco) y se dejaron solo los campos que se necesitan en la consulta.
Ojo!!!! Revisar índices, puede ayudar a optimizar mas.
explain analyze select d_pauta_id,detalle_pauta.aforo_id,ciudad_nombre,producto_nombre,elemento_nombre,ele_area_i_b,ele_area_i_h,ele_area_v_b,ele_area_v_h,referencia_arte,fecha_entrega,observa1,observa3,fecha_recepcion,fecha_programada from detalle_pauta,aforo,productos,locaciones,ciudades,elementos,elements where detalle_pauta.aforo_id=aforo.aforo_id and aforo.loc_id=locaciones.loc_id and aforo.producto_id=productos.producto_id and productos.producto_id=locaciones.producto_id and locaciones.ciudad_id=ciudades.ciudad_id and aforo.ele_id=elements.ele_id and elementos.elemento_id=elements.elemento_id and detalle_pauta.nov_id=2682
- Nested Loop (cost=45.72..516.94 rows=1 width=160) (actual time=7.285..9.462 rows=13 loops=1)
- Join Filter: ("inner".elemento_id = "outer".elemento_id)
- -> Nested Loop (cost=45.72..514.54 rows=1 width=147) (actual time=7.207..7.488 rows=13 loops=1)
- -> Hash Join (cost=45.72..508.70 rows=1 width=131) (actual time=7.198..7.373 rows=13 loops=1)
- Hash Cond: (("outer".loc_id = "inner".loc_id) AND ("outer".producto_id = "inner".producto_id))
- -> Nested Loop (cost=0.00..462.42 rows=54 width=62) (actual time=0.021..0.164 rows=13 loops=1)
- -> Index Scan using novedad_detalle_pauta on detalle_pauta (cost=0.00..138.06 rows=54 width=50) (actual time=0.011..0.039 rows=13 loops=1)
- Index Cond: (nov_id = 2682)
- -> Index Scan using aforo_pkey on aforo (cost=0.00..5.99 rows=1 width=16) (actual time=0.005..0.006 rows=1 loops=13)
- Index Cond: ("outer".aforo_id = aforo.aforo_id)
- -> Hash (cost=45.58..45.58 rows=28 width=89) (actual time=7.167..7.167 rows=1059 loops=1)
- -> Hash Join (cost=2.89..45.58 rows=28 width=89) (actual time=0.229..5.654 rows=1059 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=1.16..43.10 rows=95 width=35) (actual time=0.045..3.386 rows=1059 loops=1)
- Hash Cond: ("outer".producto_id = "inner".producto_id)
- -> Seq Scan on locaciones (cost=0.00..33.66 rows=1466 width=12) (actual time=0.001..1.129 rows=1059 loops=1)
- -> Hash (cost=1.13..1.13 rows=13 width=23) (actual time=0.036..0.036 rows=13 loops=1)
- -> Seq Scan on productos (cost=0.00..1.13 rows=13 width=23) (actual time=0.002..0.016 rows=13 loops=1)
- -> Hash (cost=1.58..1.58 rows=58 width=62) (actual time=0.168..0.168 rows=65 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.002..0.078 rows=65 loops=1)
- -> Index Scan using elements_pkey on elements (cost=0.00..5.83 rows=1 width=24) (actual time=0.004..0.005 rows=1 loops=13)
- Index Cond: ("outer".ele_id = elements.ele_id)
- -> Seq Scan on elementos (cost=0.00..1.62 rows=62 width=21) (actual time=0.002..0.081 rows=78 loops=13)
- Total runtime: 9.609 ms
Segunda consulta: Para treaer las ordenes de trabajo de los detalles si es que hay
Original:
explain analyze select a.*, b.* from ordenes a, detalle_ot b where a.ord_id=b.ord_id and a.nov_id=2715 and a.ord_estado=1 and a.ord_anulada<>1 and b.d_pauta_id=3030;
- Nested Loop (cost=7.24..83.52 rows=1 width=161) (actual time=9.564..9.564 rows=0 loops=1)
- -> Bitmap Heap Scan on ordenes a (cost=2.08..74.36 rows=1 width=151) (actual time=0.076..0.078 rows=1 loops=1)
- Recheck Cond: (nov_id = 2715)
- Filter: ((ord_estado = 1) AND (ord_anulada <> 1))
- -> Bitmap Index Scan on nov_ordenes (cost=0.00..2.08 rows=24 width=0) (actual time=0.047..0.047 rows=1 loops=1)
- Index Cond: (nov_id = 2715)
- -> Bitmap Heap Scan on detalle_ot b (cost=5.16..9.16 rows=1 width=10) (actual time=9.479..9.479 rows=0 loops=1)
- Recheck Cond: ((b.d_pauta_id = 3030) AND ("outer".ord_id = b.ord_id))
- -> BitmapAnd (cost=5.16..5.16 rows=1 width=0) (actual time=9.475..9.475 rows=0 loops=1)
- -> Bitmap Index Scan on detalle_pauta_detalle_ot (cost=0.00..2.45 rows=129 width=0) (actual time=9.157..9.157 rows=2 loops=1)
- Index Cond: (d_pauta_id = 3030)
- -> Bitmap Index Scan on ord_id_detalle_ot (cost=0.00..2.45 rows=129 width=0) (actual time=0.313..0.313 rows=0 loops=1)
- Index Cond: ("outer".ord_id = b.ord_id)
- Total runtime: 9.647 ms
Optimizada:
Ojo!!!! Revisar índices, puede ayudar a optimizar mas.
explain analyze select ord_consecutivo,d_ot_id,ordenes.ord_id from ordenes, detalle_ot where ordenes.ord_id=detalle_ot.ord_id and ordenes.nov_id=2715 and ordenes.ord_estado=1 and ordenes.ord_anulada<>1 and detalle_ot.d_pauta_id=3030;
- Nested Loop (cost=7.24..83.52 rows=1 width=12) (actual time=0.044..0.044 rows=0 loops=1)
- -> Bitmap Heap Scan on ordenes (cost=2.08..74.36 rows=1 width=8) (actual time=0.019..0.020 rows=1 loops=1)
- Recheck Cond: (nov_id = 2715)
- Filter: ((ord_estado = 1) AND (ord_anulada <> 1))
- -> Bitmap Index Scan on nov_ordenes (cost=0.00..2.08 rows=24 width=0) (actual time=0.010..0.010 rows=1 loops=1)
- Index Cond: (nov_id = 2715)
- -> Bitmap Heap Scan on detalle_ot (cost=5.16..9.16 rows=1 width=6) (actual time=0.017..0.017 rows=0 loops=1)
- Recheck Cond: ((detalle_ot.d_pauta_id = 3030) AND ("outer".ord_id = detalle_ot.ord_id))
- -> BitmapAnd (cost=5.16..5.16 rows=1 width=0) (actual time=0.015..0.015 rows=0 loops=1)
- -> Bitmap Index Scan on detalle_pauta_detalle_ot (cost=0.00..2.45 rows=129 width=0) (actual time=0.006..0.006 rows=2 loops=1)
- Index Cond: (d_pauta_id = 3030)
- -> Bitmap Index Scan on ord_id_detalle_ot (cost=0.00..2.45 rows=129 width=0) (actual time=0.006..0.006 rows=0 loops=1)
- Index Cond: ("outer".ord_id = detalle_ot.ord_id)
- Total runtime: 0.117 ms
Tercera consulta: Para saber si hay fotos subidas
Original:
explain analyze select * from detalle_foto where d_ot_id=291;
- Seq Scan on detalle_foto (cost=0.00..215.89 rows=46 width=55) (actual time=5.468..5.468 rows=0 loops=1)
- Filter: (d_ot_id = 291)
- Total runtime: 5.491 ms
Optimizada:
Ojo!!!! Revisar índices, puede ayudar a optimizar mas.
explain analyze select ord_img_name from detalle_foto where d_ot_id=291;
- Seq Scan on detalle_foto (cost=0.00..215.89 rows=46 width=43) (actual time=3.958..3.958 rows=0 loops=1)
- Filter: (d_ot_id = 291)
- Total runtime: 3.975 ms
#########################################################################################################
(PRODUCCION):
Primera consulta: Para traer todo el detalle de producción
Original:
explain analyze select a.*,ciudades.*,elements.*,elementos.*, b.nov_id, b.prueba_color from detalle_prod a, footer_prod b,ciudades,elements,elementos where a.nov_id=b.nov_id and a.nov_id=4571 and a.ele_id=elements.ele_id and elementos.elemento_id=elements.elemento_id and a.ciudad_id=ciudades.ciudad_id and a.d_realizado<>1;
- Hash Join (cost=235.20..993.96 rows=24141 width=381) (actual time=8.431..11.469 rows=120 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=91.65..525.91 rows=671 width=307) (actual time=3.268..6.012 rows=120 loops=1)
- Hash Cond: ("outer".elemento_id = "inner".elemento_id)
- -> Hash Join (cost=89.88..514.07 rows=671 width=241) (actual time=3.080..5.526 rows=120 loops=1)
- Hash Cond: ("outer".ele_id = "inner".ele_id)
- -> Index Scan using nov_id_detalle_prod on detalle_prod a (cost=0.00..331.42 rows=671 width=183) (actual time=0.292..2.407 rows=120 loops=1)
- Index Cond: (nov_id = 4571)
- Filter: (d_realizado <> 1)
- -> Hash (cost=83.70..83.70 rows=2470 width=58) (actual time=2.776..2.776 rows=793 loops=1)
- -> Seq Scan on elements (cost=0.00..83.70 rows=2470 width=58) (actual time=0.020..1.852 rows=793 loops=1)
- -> Hash (cost=1.62..1.62 rows=62 width=66) (actual time=0.175..0.175 rows=78 loops=1)
- -> Seq Scan on elementos (cost=0.00..1.62 rows=62 width=66) (actual time=0.003..0.083 rows=78 loops=1)
- -> Hash (cost=137.89..137.89 rows=2262 width=74) (actual time=5.142..5.142 rows=65 loops=1)
- -> Nested Loop (cost=3.78..137.89 rows=2262 width=74) (actual time=4.745..5.034 rows=65 loops=1)
- -> Bitmap Heap Scan on footer_prod b (cost=2.14..91.01 rows=39 width=12) (actual time=4.735..4.736 rows=1 loops=1)
- Recheck Cond: (4571 = nov_id)
- -> Bitmap Index Scan on no_id_footer (cost=0.00..2.14 rows=39 width=0) (actual time=4.709..4.709 rows=1 loops=1)
- Index Cond: (4571 = nov_id)
- -> Materialize (cost=1.64..2.22 rows=58 width=62) (actual time=0.006..0.188 rows=65 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.003..0.073 rows=65 loops=1)
- Total runtime: 11.795 ms
Optimizada:
Se crearon tres índices de búsqueda en "detalle_prod" id_detalle_prod, nov_id_detalle_prod, realizado_detalle_prod.
Se creó un índice de búsqueda en "footer_prod" no_id_footer.
Se quitó el comodín *(asterisco) y se dejaron solo los campos que se necesitan en la consulta.
explain analyze select d_pauta_id, d_prod_id, ciudad_nombre, elemento_nombre, ele_area_i_b, ele_area_i_h, ele_area_v_b, ele_area_v_h, referencia_arte, fecha_recepcion, prueba_color, fecha_aprobacion_color, fecha_programada, fecha_entrega from detalle_prod, footer_prod,ciudades,elements,elementos where detalle_prod.nov_id=footer_prod.nov_id and detalle_prod.nov_id=4571 and detalle_prod.ele_id=elements.ele_id and elementos.elemento_id=elements.elemento_id and detalle_prod.ciudad_id=ciudades.ciudad_id and detalle_prod.d_realizado<>1;
- Nested Loop (cost=539.96..1111.65 rows=24141 width=134) (actual time=3.424..5.083 rows=120 loops=1)
- -> Bitmap Heap Scan on footer_prod (cost=2.14..91.01 rows=39 width=12) (actual time=0.020..0.021 rows=1 loops=1)
- Recheck Cond: (4571 = nov_id)
- -> Bitmap Index Scan on no_id_footer (cost=0.00..2.14 rows=39 width=0) (actual time=0.016..0.016 rows=1 loops=1)
- Index Cond: (4571 = nov_id)
- -> Materialize (cost=537.82..544.01 rows=619 width=134) (actual time=3.399..4.820 rows=120 loops=1)
- -> Hash Join (cost=93.38..537.20 rows=619 width=134) (actual time=3.395..4.487 rows=120 loops=1)
- Hash Cond: ("outer".elemento_id = "inner".elemento_id)
- -> Hash Join (cost=91.60..526.14 rows=619 width=121) (actual time=3.176..4.010 rows=120 loops=1)
- Hash Cond: ("outer".ele_id = "inner".ele_id)
- -> Hash Join (cost=1.73..342.69 rows=619 width=105) (actual time=0.228..0.804 rows=120 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Index Scan using nov_id_detalle_prod on detalle_prod (cost=0.00..331.42 rows=671 width=51) (actual time=0.047..0.371 rows=120 loops=1)
- Index Cond: (nov_id = 4571)
- Filter: (d_realizado <> 1)
- -> Hash (cost=1.58..1.58 rows=58 width=62) (actual time=0.171..0.171 rows=65 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.003..0.089 rows=65 loops=1)
- -> Hash (cost=83.70..83.70 rows=2470 width=24) (actual time=2.928..2.928 rows=793 loops=1)
- -> Seq Scan on elements (cost=0.00..83.70 rows=2470 width=24) (actual time=0.015..1.830 rows=793 loops=1)
- -> Hash (cost=1.62..1.62 rows=62 width=21) (actual time=0.210..0.210 rows=78 loops=1)
- -> Seq Scan on elementos (cost=0.00..1.62 rows=62 width=21) (actual time=0.004..0.105 rows=78 loops=1)
- Total runtime: 5.314 ms
Segunda consulta: Para traer el consecutivo de la orden de producción
Original:
explain analyze select a.*, b.* from ordenes a, detalle_op b where a.ord_id=b.ord_id and a.nov_id=4571 and a.ord_anulada<>1 and a.ord_estado=1 and b.d_prod_id=4678;
- Nested Loop (cost=4.23..181.29 rows=1 width=197) (actual time=1.633..1.697 rows=1 loops=1)
- Join Filter: ("outer".ord_id = "inner".ord_id)
- -> Bitmap Heap Scan on ordenes a (cost=2.08..74.36 rows=1 width=151) (actual time=0.063..0.081 rows=3 loops=1)
- Recheck Cond: (nov_id = 4571)
- Filter: ((ord_anulada <> 1) AND (ord_estado = 1))
- -> Bitmap Index Scan on nov_ordenes (cost=0.00..2.08 rows=24 width=0) (actual time=0.036..0.036 rows=10 loops=1)
- Index Cond: (nov_id = 4571)
- -> Bitmap Heap Scan on detalle_op b (cost=2.15..106.41 rows=42 width=46) (actual time=0.518..0.530 rows=3 loops=3)
- Recheck Cond: (d_prod_id = 4678)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.15 rows=42 width=0) (actual time=0.510..0.510 rows=3 loops=3)
- Index Cond: (d_prod_id = 4678)
- Total runtime: 1.766 ms
Optimizada:
Se creó un índice de búsqueda en "detalle_op" detalle_prod_op.
Se quitó el comodín *(asterisco) y se dejaron solo los campos que se necesitan en la consulta.
explain analyze select ord_consecutivo from ordenes a, detalle_op b where a.ord_id=b.ord_id and a.nov_id=4571 and a.ord_anulada<>1 and a.ord_estado=1 and b.d_prod_id=4678;
- Nested Loop (cost=4.23..181.29 rows=1 width=4) (actual time=0.038..0.088 rows=1 loops=1)
- Join Filter: ("outer".ord_id = "inner".ord_id)
- -> Bitmap Heap Scan on ordenes a (cost=2.08..74.36 rows=1 width=8) (actual time=0.017..0.024 rows=3 loops=1)
- Recheck Cond: (nov_id = 4571)
- Filter: ((ord_anulada <> 1) AND (ord_estado = 1))
- -> Bitmap Index Scan on nov_ordenes (cost=0.00..2.08 rows=24 width=0) (actual time=0.011..0.011 rows=10 loops=1)
- Index Cond: (nov_id = 4571)
- -> Bitmap Heap Scan on detalle_op b (cost=2.15..106.41 rows=42 width=2) (actual time=0.010..0.015 rows=3 loops=3)
- Recheck Cond: (d_prod_id = 4678)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.15 rows=42 width=0) (actual time=0.006..0.006 rows=3 loops=3)
- Index Cond: (d_prod_id = 4678)
- Total runtime: 0.136 ms
#########################################################################################################
(PAUTA - PRODUCCION):
Primera consulta: Para traer todo el detalle de pauta-producción
Original:
explain analyze select distinct(b.nov_id),detalle_pauta.*,a.*,elements.*,locaciones.*,elementos.*,ciudades.*,productos.*,b.nov_id, b.prueba_color from detalle_prod a,footer_prod b,detalle_pauta,aforo,locaciones,ciudades,elementos,elements,productos where a.d_pauta_id=detalle_pauta.d_pauta_id and a.nov_id=b.nov_id and a.nov_id=4571 and a.d_realizado<>1 and detalle_pauta.aforo_id=aforo.aforo_id and aforo.loc_id=locaciones.loc_id and locaciones.ciudad_id=ciudades.ciudad_id and aforo.producto_id=productos.producto_id and productos.producto_id=locaciones.producto_id and aforo.ele_id=elements.ele_id and elementos.elemento_id=elements.elemento_id and a.ele_id=elements.ele_id;
- Unique (cost=2658.93..2666.05 rows=39 width=676) (actual time=3862.892..3863.912 rows=120 loops=1)
- -> Sort (cost=2658.93..2659.03 rows=39 width=676) (actual time=3862.889..3862.980 rows=120 loops=1)
- Sort Key: b.nov_id, detalle_pauta.d_pauta_id, detalle_pauta.nov_id, detalle_pauta.aforo_id, detalle_pauta.referencia_arte, detalle_pauta.proveedor_mat, detalle_pauta.vr_pauta, detalle_pauta.aprobacion, detalle_pauta.fecha_recepcion, detalle_pauta.fecha_programada, detalle_pauta.fecha_entrega, detalle_pauta.no_orden, detalle_pauta.recepcion_usr, detalle_pauta.item_no, detalle_pauta.d_anulado, detalle_pauta.d_renovado, detalle_pauta.d_instalado, detalle_pauta.d_desinstalado, detalle_pauta.d_orden, detalle_pauta.d_estado, detalle_pauta.observa1, detalle_pauta.observa3, a.d_prod_id, a.d_pauta_id, a.nov_id, a.ele_id, a.ciudad_id, a.prod_cant, a.datos_entrega_mat, a.referencia_arte, a.vr_prod, a.fecha_recepcion, a.fecha_programada, a.fecha_entrega, a.no_orden, a.item_no, a.recepcion_usr, a.d_realizado, a.d_orden, a.d_instalado, a.d_desinstalado, a.aprobacion, a.fecha_aprobacion_color, a.observa1, a.observa2, elements.ele_id, elements.elemento_id, elements.ele_area_i_b, elements.ele_area_i_h, elements.ele_area_v_b, elements.ele_area_v_h, elements.final_id, elements.ele_ficha, elements.mat_id, elements.res_id, elements.tinta_id, elements.tarifa_prod, locaciones.loc_id, locaciones.producto_id, locaciones.ciudad_id, locaciones.loc_nombre, locaciones.loc_plano, elementos.elemento_id, elementos.elemento_nombre, elementos.inspeccion, elementos.observacion, ciudades.ciudad_id, ciudades.ciudad_nombre, productos.producto_id, productos.producto_nombre, b.nov_id, b.prueba_color
- -> Nested Loop (cost=39.44..2657.90 rows=39 width=676) (actual time=2239.476..3861.596 rows=120 loops=1)
- -> Nested Loop (cost=37.30..2566.50 rows=1 width=664) (actual time=2239.398..3859.604 rows=120 loops=1)
- Join Filter: ("inner".elemento_id = "outer".elemento_id)
- -> Nested Loop (cost=37.30..2564.10 rows=1 width=598) (actual time=2239.367..3840.882 rows=120 loops=1)
- -> Nested Loop (cost=37.30..2558.26 rows=1 width=544) (actual time=2239.288..3839.473 rows=120 loops=1)
- Join Filter: ("inner".ele_id = "outer".ele_id)
- -> Nested Loop (cost=37.30..2211.67 rows=102 width=361) (actual time=5.857..2884.262 rows=48521 loops=1)
- -> Nested Loop (cost=35.29..1488.91 rows=41 width=241) (actual time=5.750..2171.374 rows=13234 loops=1)
- -> Hash Join (cost=2.89..45.58 rows=28 width=233) (actual time=0.239..7.644 rows=1059 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=1.16..43.10 rows=95 width=171) (actual time=0.073..4.847 rows=1059 loops=1)
- Hash Cond: ("outer".producto_id = "inner".producto_id)
- -> Seq Scan on locaciones (cost=0.00..33.66 rows=1466 width=148) (actual time=0.005..1.795 rows=1059 loops=1)
- -> Hash (cost=1.13..1.13 rows=13 width=23) (actual time=0.046..0.046 rows=13 loops=1)
- -> Seq Scan on productos (cost=0.00..1.13 rows=13 width=23) (actual time=0.002..0.021 rows=13 loops=1)
- -> Hash (cost=1.58..1.58 rows=58 width=62) (actual time=0.149..0.149 rows=65 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.002..0.071 rows=65 loops=1)
- -> Bitmap Heap Scan on aforo (cost=32.40..51.47 rows=5 width=16) (actual time=1.902..2.016 rows=12 loops=1059)
- Recheck Cond: ((aforo.loc_id = "outer".loc_id) AND (aforo.producto_id = "outer".producto_id))
- -> BitmapAnd (cost=32.40..32.40 rows=5 width=0) (actual time=1.877..1.877 rows=0 loops=1059)
- -> Bitmap Index Scan on loca_id_aforo (cost=0.00..2.21 rows=60 width=0) (actual time=0.025..0.025 rows=74 loops=1059)
- Index Cond: (aforo.loc_id = "outer".loc_id)
- -> Bitmap Index Scan on producto_aforo (cost=0.00..29.94 rows=2555 width=0) (actual time=1.998..1.998 rows=10425 loops=978)
- Index Cond: (aforo.producto_id = "outer".producto_id)
- -> Bitmap Heap Scan on detalle_pauta (cost=2.01..17.58 rows=4 width=124) (actual time=0.018..0.044 rows=4 loops=13234)
- Recheck Cond: (detalle_pauta.aforo_id = "outer".aforo_id)
- -> Bitmap Index Scan on aforo_detalle_pauta (cost=0.00..2.01 rows=4 width=0) (actual time=0.009..0.009 rows=13 loops=13234)
- Index Cond: (detalle_pauta.aforo_id = "outer".aforo_id)
- -> Index Scan using detalle_pauta_produccion on detalle_prod a (cost=0.00..3.38 rows=1 width=183) (actual time=0.018..0.018 rows=0 loops=48521)
- Index Cond: (a.d_pauta_id = "outer".d_pauta_id)
- Filter: ((nov_id = 4571) AND (d_realizado <> 1))
- -> Index Scan using elements_pkey on elements (cost=0.00..5.83 rows=1 width=58) (actual time=0.006..0.007 rows=1 loops=120)
- Index Cond: ("outer".ele_id = elements.ele_id)
- -> Seq Scan on elementos (cost=0.00..1.62 rows=62 width=66) (actual time=0.002..0.084 rows=78 loops=120)
- -> Bitmap Heap Scan on footer_prod b (cost=2.14..91.01 rows=39 width=12) (actual time=0.010..0.010 rows=1 loops=120)
- Recheck Cond: (4571 = nov_id)
- -> Bitmap Index Scan on no_id_footer (cost=0.00..2.14 rows=39 width=0) (actual time=0.007..0.007 rows=1 loops=120)
- Index Cond: (4571 = nov_id)
- Total runtime: 3864.836 ms
Optimizada:
Se crearon 5 índices de búsqueda en "detalle_prod": id_detalle_prod, nov_id_detalle_prod, realizado_detalle_prod, elemento_prod, ciudad_prod.
Se creó un índice de búsqueda en "footer_prod": no_id_footer.
Se quitó el comodín *(asterisco) y se dejaron solo los campos que se necesitan en la consulta.
explain analyze select distinct(b.nov_id), d_prod_id, aforo.aforo_id, ciudad_nombre, producto_nombre, elemento_nombre, ele_area_i_b, ele_area_i_h, ele_area_v_b, ele_area_v_h, a.referencia_arte, observa1, a.d_pauta_id, a.fecha_recepcion, prueba_color, fecha_aprobacion_color, a.fecha_programada, a.fecha_entrega, observa2 from footer_prod b, detalle_pauta, detalle_prod a, aforo,locaciones,ciudades,elementos,elements,productos where a.d_pauta_id=detalle_pauta.d_pauta_id and a.nov_id=b.nov_id and a.nov_id=4571 and a.d_realizado<>1 and detalle_pauta.aforo_id=aforo.aforo_id and aforo.loc_id=locaciones.loc_id and locaciones.ciudad_id=ciudades.ciudad_id and aforo.producto_id=productos.producto_id and productos.producto_id=locaciones.producto_id and aforo.ele_id=elements.ele_id and elementos.elemento_id=elements.elemento_id and a.ele_id=elements.ele_id;
Segunda consulta: Para traer las ordenes de produccion
Original:
explain analyze select a.*, b.* from ordenes a, detalle_op b where a.ord_id=b.ord_id and a.nov_id=4571 and a.ord_estado=1 and a.ord_tipo=2 and a.ord_anulada<>1 and b.d_prod_id=4600;
- Nested Loop (cost=4.23..181.35 rows=1 width=197) (actual time=9.785..9.805 rows=1 loops=1)
- Join Filter: ("outer".ord_id = "inner".ord_id)
- -> Bitmap Heap Scan on ordenes a (cost=2.08..74.42 rows=1 width=151) (actual time=0.083..0.100 rows=1 loops=1)
- Recheck Cond: (nov_id = 4571)
- Filter: ((ord_estado = 1) AND (ord_tipo = 2) AND (ord_anulada <> 1))
- -> Bitmap Index Scan on nov_ordenes (cost=0.00..2.08 rows=24 width=0) (actual time=0.057..0.057 rows=10 loops=1)
- Index Cond: (nov_id = 4571)
- -> Bitmap Heap Scan on detalle_op b (cost=2.15..106.41 rows=42 width=46) (actual time=9.672..9.690 rows=6 loops=1)
- Recheck Cond: (d_prod_id = 4600)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.15 rows=42 width=0) (actual time=9.652..9.652 rows=6 loops=1)
- Index Cond: (d_prod_id = 4600)
- Total runtime: 9.885 ms
Optimizada:
Se quitó el comodín *(asterisco) y se dejaron solo los campos que se necesitan en la consulta.
explain analyze select ord_consecutivo from ordenes a, detalle_op b where a.ord_id=b.ord_id and a.nov_id=4571 and a.ord_estado=1 and a.ord_tipo=2 and a.ord_anulada<>1 and b.d_prod_id=4600;
- Nested Loop (cost=4.23..181.35 rows=1 width=4) (actual time=0.044..0.052 rows=1 loops=1)
- Join Filter: ("outer".ord_id = "inner".ord_id)
- -> Bitmap Heap Scan on ordenes a (cost=2.08..74.42 rows=1 width=8) (actual time=0.017..0.022 rows=1 loops=1)
- Recheck Cond: (nov_id = 4571)
- Filter: ((ord_estado = 1) AND (ord_tipo = 2) AND (ord_anulada <> 1))
- -> Bitmap Index Scan on nov_ordenes (cost=0.00..2.08 rows=24 width=0) (actual time=0.011..0.011 rows=10 loops=1)
- Index Cond: (nov_id = 4571)
- -> Bitmap Heap Scan on detalle_op b (cost=2.15..106.41 rows=42 width=2) (actual time=0.011..0.018 rows=6 loops=1)
- Recheck Cond: (d_prod_id = 4600)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.15 rows=42 width=0) (actual time=0.007..0.007 rows=6 loops=1)
- Index Cond: (d_prod_id = 4600)
- Total runtime: 0.101 ms
Tercera consulta: Para traer los datelles de pauta
Original:
explain analyze select * from detalle_pauta where d_pauta_id=14959
- Seq Scan on detalle_pauta (cost=0.00..3563.78 rows=1 width=124) (actual time=0.267..54.708 rows=1 loops=1)
- Filter: (d_pauta_id = 14959)
- Total runtime: 54.747 ms
Optimizada:
Se quitó el comodín *(asterisco) y se dejaron solo los campos que se necesitan en la consulta.
explain analyze select fecha_recepcion, d_pauta_id, fecha_programada, fecha_entrega, observa3 from detalle_pauta where d_pauta_id=14959;