Re: Agilizar consultas sobre fechas

From: Calabaza <calalinux(at)gmail(dot)com>
To: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: Agilizar consultas sobre fechas
Date: 2009-10-01 15:51:37
Message-ID: 958993320910010851o6e7bb643ha53012624bda0759@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

2009/10/1 Manuel Alejandro Estevez Fernandez <stvzito(at)gmail(dot)com>:
> Saludos a todos.
>
> Tengo la siguiente consulta:
>
> select vd.cantidad,vd.precio_venta_unidad_venta,vd.id_esquema_impuesto
>                        from ventas_detalle  vd, ventas v, articulos a
>                        where     v.id_sucursal=sucursal and
>                                vd.id_sucursal=v.id_sucursal and
>                                vd.id_venta=v.id_venta and
>                                cast(v.fecha as date) = fecha_revision  and
>                                vd.id_articulo = a.id_articulo and
>                                a.servicio is null
>
> La estructura de las tablas es la siguiente:
>
> CREATE TABLE ventas
> (
>  id_venta serial NOT NULL,
>  id_corte_caja bigint NOT NULL,
>  id_sucursal smallint NOT NULL,
>  id_concepto smallint NOT NULL,
>  numero_caja smallint NOT NULL,
>  folio_venta bigint,
>  folio_devolucion bigint,
>  fecha timestamp without time zone NOT NULL,
>  id_estatus smallint NOT NULL,
>  devuelto boolean,
>  sincronizado integer DEFAULT 0,
>  CONSTRAINT ventas_pkey PRIMARY KEY (id_venta, id_corte_caja, id_sucursal,
> numero_caja),
>  CONSTRAINT ventas_fk_conceptos FOREIGN KEY (id_concepto)
>      REFERENCES conceptos (id_concepto) MATCH SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION,
>  CONSTRAINT ventas_fk_corte_caja FOREIGN KEY (id_corte_caja, id_sucursal,
> numero_caja)
>      REFERENCES corte_caja (id_corte_caja, id_sucursal, numero_caja) MATCH
> SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION,
>  CONSTRAINT ventas_fk_estatus FOREIGN KEY (id_estatus)
>      REFERENCES estatus (id_estatus) MATCH SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION
> )
> WITHOUT OIDS;
> ALTER TABLE ventas OWNER TO yepas;
>
> -- Index: ventas_id_sucursal_index
>
> -- DROP INDEX ventas_id_sucursal_index;
>
> CREATE INDEX ventas_id_sucursal_index
>  ON ventas
>  USING btree
>  (id_sucursal);
>
> -- Index: ventas_id_venta_index
>
> -- DROP INDEX ventas_id_venta_index;
>
> CREATE INDEX ventas_id_venta_index
>  ON ventas
>  USING btree
>  (id_venta);
>
> CREATE TABLE ventas_detalle
> (
>  id_venta bigint NOT NULL,
>  id_sucursal smallint NOT NULL,
>  id_corte_caja bigint NOT NULL,
>  numero_caja smallint NOT NULL,
>  id_venta_detalle serial NOT NULL,
>  id_esquema_impuesto smallint NOT NULL,
>  id_esquema_oferta smallint NOT NULL,
>  cantidad numeric(12,3),
>  precio_venta_unidad_venta numeric(12,3),
>  tipo_precio text,
>  precio_regular_unidad_venta numeric(12,3),
>  precio_chequeo_unidad_venta numeric(12,3),
>  precio_oferta_unidad_venta numeric(12,3),
>  kit text,
>  id_kit smallint,
>  factor numeric(12,3),
>  costo_unitario_promedio numeric(12,3),
>  cantidad_devuelta numeric(12,3),
>  id_articulo bigint NOT NULL,
>  CONSTRAINT ventas_detalle_pkey PRIMARY KEY (id_venta, id_sucursal,
> id_corte_caja, numero_caja, id_venta_detalle),
>  CONSTRAINT esquema_ofertas_fk_ofertas FOREIGN KEY (id_esquema_oferta)
>      REFERENCES esquema_ofertas (id_esquema_oferta) MATCH SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION,
>  CONSTRAINT venta_detalle_fk_esquema_impuesto FOREIGN KEY
> (id_esquema_impuesto)
>      REFERENCES esquema_impuestos (id_esquema_impuesto) MATCH SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION,
>  CONSTRAINT ventas_detalle_fk_articulos FOREIGN KEY (id_articulo)
>      REFERENCES articulos (id_articulo) MATCH SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION,
>  CONSTRAINT ventas_detalle_fk_kits FOREIGN KEY (id_kit)
>      REFERENCES kits (id_kit) MATCH SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION,
>  CONSTRAINT ventas_detalle_fk_ventas FOREIGN KEY (id_venta, id_corte_caja,
> numero_caja, id_sucursal)
>      REFERENCES ventas (id_venta, id_corte_caja, numero_caja, id_sucursal)
> MATCH SIMPLE
>      ON UPDATE NO ACTION ON DELETE NO ACTION
> )
> WITHOUT OIDS;
> ALTER TABLE ventas_detalle OWNER TO yepas;
>
> -- Index: ix_id_esquema_oferta
>
> -- DROP INDEX ix_id_esquema_oferta;
>
> CREATE INDEX ix_id_esquema_oferta
>  ON ventas_detalle
>  USING btree
>  (id_esquema_oferta);
>
> -- Index: ventas_detalle_id_sucursal_index
>
> -- DROP INDEX ventas_detalle_id_sucursal_index;
>
> CREATE INDEX ventas_detalle_id_sucursal_index
>  ON ventas_detalle
>  USING btree
>  (id_sucursal);
>
> -- Index: ventas_detalle_id_ventas_index
>
> -- DROP INDEX ventas_detalle_id_ventas_index;
>
> CREATE INDEX ventas_detalle_id_ventas_index
>  ON ventas_detalle
>  USING btree
>  (id_venta);
>
> El problema es el tiempo de respuesta de la consulta la tabla ventas tiene
> alrededor de 36 millones de registros y la de venta detalle cerca de 76
> millones.Y para una sucursal y un día en específico tarda entre 30 a 40
> minutos. ¿Alguna sugerencia o tip para mejorar la consulta?
>
>
> --
> Atentamente.
>
> Manuel Alejandro Estévez Fernández
>
> --
> TIP 5: ¿Has leído nuestro extenso FAQ?
>        http://www.postgresql.org/docs/faqs.FAQ.html
>

Por favor envía el explain de la consulta.
También podrías actualizar las estadísticas con un analyze.

Y me parece que como no obtienes ni un campo de la tabla
de artículos podrías sacarla de la consulta.

--
§~^Calabaza^~§ from Villa Elisa, Paraguay
http://calablogbaza.blogspot.com/

http://es.wikipedia.org/wiki/Top-posting
http://es.wikipedia.org/wiki/Netiquette

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Jaime Casanova 2009-10-01 17:13:32 Re: Agilizar consultas sobre fechas
Previous Message Manuel Alejandro Estevez Fernandez 2009-10-01 15:34:18 Agilizar consultas sobre fechas