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=37.38..830.03 rows=2172 width=164) (actual time=2.202..25.457 rows=1267 loops=1)
- Hash Cond: ("outer".usuario_id = "inner".usuario_id)
- -> Hash Join (cost=34.17..794.25 rows=2172 width=100) (actual time=1.843..22.533 rows=1267 loops=1)
- Hash Cond: ("outer".cliente_id = "inner".cliente_id)
- -> Seq Scan on novedades (cost=0.00..727.50 rows=2172 width=36) (actual time=0.060..18.152 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=32.14..32.14 rows=814 width=72) (actual time=1.764..1.764 rows=535 loops=1)
- -> Seq Scan on clientes (cost=0.00..32.14 rows=814 width=72) (actual time=0.022..1.058 rows=535 loops=1)
- -> Hash (cost=2.96..2.96 rows=96 width=72) (actual time=0.211..0.211 rows=64 loops=1)
- -> Seq Scan on usuarios (cost=0.00..2.96 rows=96 width=72) (actual time=0.010..0.119 rows=64 loops=1)
- Total runtime: 26.565 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=218.01..264.84 rows=1 width=556) (actual time=19.489..22.497 rows=13 loops=1)
- Join Filter: ("inner".elemento_id = "outer".elemento_id)
- -> Nested Loop (cost=218.01..262.44 rows=1 width=490) (actual time=19.424..20.672 rows=13 loops=1)
- -> Hash Join (cost=218.01..256.60 rows=1 width=432) (actual time=19.340..20.439 rows=13 loops=1)
- Hash Cond: (("outer".loc_id = "inner".loc_id) AND ("outer".producto_id = "inner".producto_id))
- -> Hash Join (cost=2.89..41.09 rows=25 width=233) (actual time=0.299..5.759 rows=1009 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=1.16..38.69 rows=85 width=171) (actual time=0.114..3.522 rows=1009 loops=1)
- Hash Cond: ("outer".producto_id = "inner".producto_id)
- -> Seq Scan on locaciones (cost=0.00..30.12 rows=1312 width=148) (actual time=0.034..1.320 rows=1009 loops=1)
- -> Hash (cost=1.13..1.13 rows=13 width=23) (actual time=0.059..0.059 rows=13 loops=1)
- -> Seq Scan on productos (cost=0.00..1.13 rows=13 width=23) (actual time=0.021..0.034 rows=13 loops=1)
- -> Hash (cost=1.58..1.58 rows=58 width=62) (actual time=0.168..0.168 rows=64 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.023..0.087 rows=64 loops=1)
- -> Hash (cost=215.00..215.00 rows=25 width=199) (actual time=13.724..13.724 rows=13 loops=1)
- -> Nested Loop (cost=0.00..215.00 rows=25 width=199) (actual time=11.670..13.670 rows=13 loops=1)
- -> Index Scan using novedad_detalle_pauta on detalle_pauta (cost=0.00..64.86 rows=25 width=124) (actual time=8.640..10.470 rows=13 loops=1)
- Index Cond: (nov_id = 2682)
- -> Index Scan using aforo_pkey on aforo (cost=0.00..5.99 rows=1 width=75) (actual time=0.240..0.241 rows=1 loops=13)
- Index Cond: ("outer".aforo_id = aforo.aforo_id)
- -> Index Scan using elements_pkey on elements (cost=0.00..5.83 rows=1 width=58) (actual time=0.012..0.013 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.074 rows=71 loops=13)
- Total runtime: 22.907 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=218.01..264.84 rows=1 width=160) (actual time=5.589..8.466 rows=13 loops=1)
- Join Filter: ("inner".elemento_id = "outer".elemento_id)
- -> Nested Loop (cost=218.01..262.44 rows=1 width=147) (actual time=5.546..6.655 rows=13 loops=1)
- -> Hash Join (cost=218.01..256.60 rows=1 width=131) (actual time=5.536..6.532 rows=13 loops=1)
- Hash Cond: (("outer".loc_id = "inner".loc_id) AND ("outer".producto_id = "inner".producto_id))
- -> Hash Join (cost=2.89..41.09 rows=25 width=89) (actual time=0.260..5.402 rows=1009 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=1.16..38.69 rows=85 width=35) (actual time=0.068..3.238 rows=1009 loops=1)
- Hash Cond: ("outer".producto_id = "inner".producto_id)
- -> Seq Scan on locaciones (cost=0.00..30.12 rows=1312 width=12) (actual time=0.004..1.079 rows=1009 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.003..0.019 rows=13 loops=1)
- -> Hash (cost=1.58..1.58 rows=58 width=62) (actual time=0.177..0.177 rows=64 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.003..0.079 rows=64 loops=1)
- -> Hash (cost=215.00..215.00 rows=25 width=62) (actual time=0.187..0.187 rows=13 loops=1)
- -> Nested Loop (cost=0.00..215.00 rows=25 width=62) (actual time=0.020..0.153 rows=13 loops=1)
- -> Index Scan using novedad_detalle_pauta on detalle_pauta (cost=0.00..64.86 rows=25 width=50) (actual time=0.009..0.037 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.004..0.005 rows=1 loops=13)
- Index Cond: ("outer".aforo_id = aforo.aforo_id)
- -> 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.001..0.075 rows=71 loops=13)
- Total runtime: 8.683 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=6.83..62.12 rows=1 width=161) (actual time=26.728..26.728 rows=0 loops=1)
- -> Bitmap Heap Scan on ordenes a (cost=2.06..53.35 rows=1 width=151) (actual time=0.105..0.106 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.06 rows=17 width=0) (actual time=0.078..0.078 rows=1 loops=1)
- Index Cond: (nov_id = 2715)
- -> Bitmap Heap Scan on detalle_ot b (cost=4.77..8.76 rows=1 width=10) (actual time=26.615..26.615 rows=0 loops=1)
- Recheck Cond: ((b.d_pauta_id = 3030) AND ("outer".ord_id = b.ord_id))
- -> BitmapAnd (cost=4.77..4.77 rows=1 width=0) (actual time=26.613..26.613 rows=0 loops=1)
- -> Bitmap Index Scan on detalle_pauta_detalle_ot (cost=0.00..2.26 rows=74 width=0) (actual time=10.914..10.914 rows=2 loops=1)
- Index Cond: (d_pauta_id = 3030)
- -> Bitmap Index Scan on ord_id_detalle_ot (cost=0.00..2.26 rows=74 width=0) (actual time=15.695..15.695 rows=0 loops=1)
- Index Cond: ("outer".ord_id = b.ord_id)
- Total runtime: 26.802 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=6.83..62.12 rows=1 width=12) (actual time=0.033..0.033 rows=0 loops=1)
- -> Bitmap Heap Scan on ordenes (cost=2.06..53.35 rows=1 width=8) (actual time=0.012..0.013 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.06 rows=17 width=0) (actual time=0.007..0.007 rows=1 loops=1)
- Index Cond: (nov_id = 2715)
- -> Bitmap Heap Scan on detalle_ot (cost=4.77..8.76 rows=1 width=6) (actual time=0.015..0.015 rows=0 loops=1)
- Recheck Cond: ((detalle_ot.d_pauta_id = 3030) AND ("outer".ord_id = detalle_ot.ord_id))
- -> BitmapAnd (cost=4.77..4.77 rows=1 width=0) (actual time=0.013..0.013 rows=0 loops=1)
- -> Bitmap Index Scan on detalle_pauta_detalle_ot (cost=0.00..2.26 rows=74 width=0) (actual time=0.005..0.005 rows=2 loops=1)
- Index Cond: (d_pauta_id = 3030)
- -> Bitmap Index Scan on ord_id_detalle_ot (cost=0.00..2.26 rows=74 width=0) (actual time=0.006..0.006 rows=0 loops=1)
- Index Cond: ("outer".ord_id = detalle_ot.ord_id)
- Total runtime: 0.085 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..85.50 rows=18 width=55) (actual time=75.963..75.963 rows=0 loops=1)
- Filter: (d_ot_id = 291)
- Total runtime: 75.981 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..85.50 rows=18 width=43) (actual time=1.527..1.527 rows=0 loops=1)
- Filter: (d_ot_id = 291)
- Total runtime: 1.542 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=153.86..532.13 rows=6232 width=381) (actual time=12.218..21.563 rows=120 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=82.51..380.81 rows=355 width=307) (actual time=5.743..14.795 rows=120 loops=1)
- Hash Cond: ("outer".elemento_id = "inner".elemento_id)
- -> Hash Join (cost=80.74..373.71 rows=355 width=241) (actual time=5.569..14.321 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..175.67 rows=355 width=183) (actual time=3.151..11.583 rows=120 loops=1)
- Index Cond: (nov_id = 4571)
- Filter: (d_realizado <> 1)
- -> Hash (cost=75.19..75.19 rows=2219 width=58) (actual time=2.400..2.400 rows=698 loops=1)
- -> Seq Scan on elements (cost=0.00..75.19 rows=2219 width=58) (actual time=0.019..1.565 rows=698 loops=1)
- -> Hash (cost=1.62..1.62 rows=62 width=66) (actual time=0.160..0.160 rows=71 loops=1)
- -> Seq Scan on elementos (cost=0.00..1.62 rows=62 width=66) (actual time=0.002..0.075 rows=71 loops=1)
- -> Hash (cost=68.59..68.59 rows=1102 width=74) (actual time=6.459..6.459 rows=64 loops=1)
- -> Nested Loop (cost=3.70..68.59 rows=1102 width=74) (actual time=6.072..6.360 rows=64 loops=1)
- -> Bitmap Heap Scan on footer_prod b (cost=2.07..44.91 rows=19 width=12) (actual time=6.062..6.063 rows=1 loops=1)
- Recheck Cond: (4571 = nov_id)
- -> Bitmap Index Scan on no_id_footer (cost=0.00..2.07 rows=19 width=0) (actual time=0.471..0.471 rows=1 loops=1)
- Index Cond: (4571 = nov_id)
- -> Materialize (cost=1.64..2.22 rows=58 width=62) (actual time=0.006..0.186 rows=64 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.003..0.073 rows=64 loops=1)
- Total runtime: 21.869 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=391.44..558.93 rows=6232 width=134) (actual time=2.560..4.222 rows=120 loops=1)
- -> Bitmap Heap Scan on footer_prod (cost=2.07..44.91 rows=19 width=12) (actual time=0.019..0.020 rows=1 loops=1)
- Recheck Cond: (4571 = nov_id)
- -> Bitmap Index Scan on no_id_footer (cost=0.00..2.07 rows=19 width=0) (actual time=0.015..0.015 rows=1 loops=1)
- Index Cond: (4571 = nov_id)
- -> Materialize (cost=389.38..392.66 rows=328 width=134) (actual time=2.536..3.955 rows=120 loops=1)
- -> Hash Join (cost=84.24..389.05 rows=328 width=134) (actual time=2.532..3.600 rows=120 loops=1)
- Hash Cond: ("outer".elemento_id = "inner".elemento_id)
- -> Hash Join (cost=82.46..382.35 rows=328 width=121) (actual time=2.340..3.157 rows=120 loops=1)
- Hash Cond: ("outer".ele_id = "inner".ele_id)
- -> Hash Join (cost=1.73..182.45 rows=328 width=105) (actual time=0.194..0.750 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..175.67 rows=355 width=51) (actual time=0.011..0.308 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.167..0.167 rows=64 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.002..0.081 rows=64 loops=1)
- -> Hash (cost=75.19..75.19 rows=2219 width=24) (actual time=2.128..2.128 rows=698 loops=1)
- -> Seq Scan on elements (cost=0.00..75.19 rows=2219 width=24) (actual time=0.004..1.144 rows=698 loops=1)
- -> Hash (cost=1.62..1.62 rows=62 width=21) (actual time=0.182..0.182 rows=71 loops=1)
- -> Seq Scan on elementos (cost=0.00..1.62 rows=62 width=21) (actual time=0.003..0.095 rows=71 loops=1)
- Total runtime: 4.446 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.18..140.41 rows=1 width=197) (actual time=8.266..8.333 rows=1 loops=1)
- Join Filter: ("outer".ord_id = "inner".ord_id)
- -> Bitmap Heap Scan on ordenes a (cost=2.06..53.35 rows=1 width=151) (actual time=6.429..6.448 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.06 rows=17 width=0) (actual time=6.406..6.406 rows=10 loops=1)
- Index Cond: (nov_id = 4571)
- -> Bitmap Heap Scan on detalle_op b (cost=2.12..86.64 rows=34 width=46) (actual time=0.608..0.619 rows=3 loops=3)
- Recheck Cond: (d_prod_id = 4678)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.12 rows=34 width=0) (actual time=0.599..0.599 rows=3 loops=3)
- Index Cond: (d_prod_id = 4678)
- Total runtime: 8.399 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.18..140.41 rows=1 width=4) (actual time=0.038..0.087 rows=1 loops=1)
- Join Filter: ("outer".ord_id = "inner".ord_id)
- -> Bitmap Heap Scan on ordenes a (cost=2.06..53.35 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.06 rows=17 width=0) (actual time=0.010..0.010 rows=10 loops=1)
- Index Cond: (nov_id = 4571)
- -> Bitmap Heap Scan on detalle_op b (cost=2.12..86.64 rows=34 width=2) (actual time=0.010..0.014 rows=3 loops=3)
- Recheck Cond: (d_prod_id = 4678)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.12 rows=34 width=0) (actual time=0.006..0.006 rows=3 loops=3)
- Index Cond: (d_prod_id = 4678)
- Total runtime: 0.134 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=1403.15..1406.61 rows=19 width=676) (actual time=1946.955..1947.992 rows=120 loops=1)
- -> Sort (cost=1403.15..1403.19 rows=19 width=676) (actual time=1946.952..1947.041 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=24.59..1402.74 rows=19 width=676) (actual time=1249.019..1945.729 rows=120 loops=1)
- -> Nested Loop (cost=22.52..1357.64 rows=1 width=664) (actual time=1248.941..1943.812 rows=120 loops=1)
- Join Filter: ("inner".elemento_id = "outer".elemento_id)
- -> Nested Loop (cost=22.52..1355.25 rows=1 width=598) (actual time=1248.908..1926.762 rows=120 loops=1)
- -> Nested Loop (cost=22.52..1349.40 rows=1 width=544) (actual time=1248.838..1925.387 rows=120 loops=1)
- Join Filter: ("inner".ele_id = "outer".ele_id)
- -> Nested Loop (cost=22.52..1186.31 rows=48 width=361) (actual time=2.433..1522.044 rows=26495 loops=1)
- -> Nested Loop (cost=20.51..768.62 rows=24 width=241) (actual time=2.327..944.850 rows=12648 loops=1)
- -> Hash Join (cost=2.89..41.09 rows=25 width=233) (actual time=0.231..6.798 rows=1009 loops=1)
- Hash Cond: ("outer".ciudad_id = "inner".ciudad_id)
- -> Hash Join (cost=1.16..38.69 rows=85 width=171) (actual time=0.068..4.285 rows=1009 loops=1)
- Hash Cond: ("outer".producto_id = "inner".producto_id)
- -> Seq Scan on locaciones (cost=0.00..30.12 rows=1312 width=148) (actual time=0.005..1.605 rows=1009 loops=1)
- -> Hash (cost=1.13..1.13 rows=13 width=23) (actual time=0.042..0.042 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.148..0.148 rows=64 loops=1)
- -> Seq Scan on ciudades (cost=0.00..1.58 rows=58 width=62) (actual time=0.002..0.068 rows=64 loops=1)
- -> Bitmap Heap Scan on aforo (cost=17.62..29.06 rows=3 width=16) (actual time=0.860..0.903 rows=13 loops=1009)
- Recheck Cond: ((aforo.loc_id = "outer".loc_id) AND (aforo.producto_id = "outer".producto_id))
- -> BitmapAnd (cost=17.62..17.62 rows=3 width=0) (actual time=0.850..0.850 rows=0 loops=1009)
- -> Bitmap Index Scan on loca_id_aforo (cost=0.00..2.12 rows=35 width=0) (actual time=0.014..0.014 rows=34 loops=1009)
- Index Cond: (aforo.loc_id = "outer".loc_id)
- -> Bitmap Index Scan on producto_aforo (cost=0.00..15.25 rows=1500 width=0) (actual time=0.904..0.904 rows=4697 loops=928)
- Index Cond: (aforo.producto_id = "outer".producto_id)
- -> Bitmap Heap Scan on detalle_pauta (cost=2.01..17.35 rows=4 width=124) (actual time=0.030..0.039 rows=2 loops=12648)
- 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.024..0.024 rows=6 loops=12648)
- 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.013..0.013 rows=0 loops=26495)
- 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.075 rows=71 loops=120)
- -> Bitmap Heap Scan on footer_prod b (cost=2.07..44.91 rows=19 width=12) (actual time=0.009..0.010 rows=1 loops=120)
- Recheck Cond: (4571 = nov_id)
- -> Bitmap Index Scan on no_id_footer (cost=0.00..2.07 rows=19 width=0) (actual time=0.006..0.006 rows=1 loops=120)
- Index Cond: (4571 = nov_id)
- Total runtime: 1948.894 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.18..140.46 rows=1 width=197) (actual time=8.455..8.474 rows=1 loops=1)
- Join Filter: ("outer".ord_id = "inner".ord_id)
- -> Bitmap Heap Scan on ordenes a (cost=2.06..53.39 rows=1 width=151) (actual time=0.084..0.101 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.06 rows=17 width=0) (actual time=0.058..0.058 rows=10 loops=1)
- Index Cond: (nov_id = 4571)
- -> Bitmap Heap Scan on detalle_op b (cost=2.12..86.64 rows=34 width=46) (actual time=8.339..8.356 rows=6 loops=1)
- Recheck Cond: (d_prod_id = 4600)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.12 rows=34 width=0) (actual time=8.318..8.318 rows=6 loops=1)
- Index Cond: (d_prod_id = 4600)
- Total runtime: 8.555 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.18..140.46 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.06..53.39 rows=1 width=8) (actual time=0.018..0.023 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.06 rows=17 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.12..86.64 rows=34 width=2) (actual time=0.012..0.019 rows=6 loops=1)
- Recheck Cond: (d_prod_id = 4600)
- -> Bitmap Index Scan on detalle_prod_op (cost=0.00..2.12 rows=34 width=0) (actual time=0.008..0.008 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..1676.91 rows=1 width=124) (actual time=0.263..24.290 rows=1 loops=1)
- Filter: (d_pauta_id = 14959)
- Total runtime: 24.328 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;