Re: Ayuda con inquietud

From: Jorge Toro <jolthgs(at)gmail(dot)com>
To: Alejandro Carrillo <fasterzip(at)yahoo(dot)es>
Cc: Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu>, Foro Postgres <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Ayuda con inquietud
Date: 2012-07-09 16:13:12
Message-ID: CAK0+Q89NomxBS2_ex7qCtAHG6j6UXqFRhPkgJV7BmWU1hR-E+w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Hola Alejandro, gracias por tu ayuda.

Lo que entiendo de tu propuesta es hacer:

gps
-----------
id serial PK
name text
type text
active boolean
position_id integer -- índice a la última posición del gps.

positions
--------------
id serial PK
gps_id integer FK (gps.id)
posicion point
velocidad real
altura real
date timestamp
grados real
satelites integer

CREATE INDEX "gps_position_id" ON "gps" ("position_id");
CREATE INDEX "positions_gps_id" ON "positions" ("gps_id");

Gracias de antemano.

*Jorge Alonso Toro*
Ing. Teleinformático.

http://jolthgs.wordpress.com/
www.devmicrosystem.com
--------------------------------------------------------------
Powered By Debian.
Developer Bullix GNU/Linux.
--------------------------------------------------------------
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBIWWH6q7mzdgTzI5ARAkX5AJ9TR6hL2ocLMOUDRfhts8DlVl+jpwCeNw5x
p4+4FNUHPDUx1lU9F8WSKCA=
=zRhQ
-----END PGP SIGNATURE-----
Este correo esta protegido bajo los términos de la Licencia
Atribución-Compartir Obras Derivadas Igual a 2.5 Colombia de Creative
Commons. Observé la licencia visitando este sitio
http://creativecommons.org/licenses/by-sa/2.5/co/.

El 9 de julio de 2012 10:34, Alejandro Carrillo <fasterzip(at)yahoo(dot)es>escribió:

> Creo que lo mejor es hacer un índice único en la tabla GPS (llave
> primaria), un campo donde esté la última posición del GPS y otro índice en
> la llave foránea de GPS en la tabla positions. Y crear una function asi:
>
> CREATE OR REPLACE FUNCTION public.fn_guardar_posicion (
>
> gps_id_ integer,
> posicion_ varchar,
> velocidad_ real,
> altura_ real,
> fecha_ timestamp,
> grados_ real,
> satelites_ integer
> )
> RETURNS integer AS
> $body$
> DECLARE
> retorno INTEGER;
> BEGIN
>
> INSERT INTO positions
> (
> gps_id,
> posicion,
> velocidad,
> altura,
> fecha,
> grados,
> satelites
> )
> VALUES (
> gps_id_,
> posicion_,
> velocidad_,
> altura_,
> fecha_,
> grados_,
> satelites_
> )RETURNING id INTO retorno;
> if (id is null) then
> UPDATE gps
> SET
> posicion = posicion_,
> WHERE id = gps_id_;
> end if;
> ;
>
> RETURN retorno;
> END;
> $body$
> LANGUAGE 'plpgsql';
>
> Así no necesitas tener más tablas adicionales. No olvidar hacer vacuum
> full a las tablas periódicamente en horario donde haya pocas transacciones.
>
> ------------------------------
> *De:* Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu>
> *Para:* Jorge Toro <jolthgs(at)gmail(dot)com>; Foro Postgres <
> pgsql-es-ayuda(at)postgresql(dot)org>
> *Enviado:* Lunes 9 de julio de 2012 10:21
> *Asunto:* RE: [pgsql-es-ayuda] Ayuda con inquietud
>
> Entonces podrias tener dos tablas positions, una que se llame
> hist_positions por ejemplo, y la otra positions, sobre positions almacenas
> la última ubicación de los gps y sobre hist_positions el historial de
> posiciones de los gps, para almacenar las posiciones de los gps utililizas
> una función, y para llevar el historial de los gps, puedes utilizar un
> trigger sobre positions. Acá te dejo el código de todo, solo falta la
> referencia de la clave foránea. Espero que te sirva.
>
> PD: Pudieran existir otras soluciones.
>
> --Crear la tabla positions
> CREATE TABLE public.positions (
> id SERIAL,
> gps_id INTEGER,
> posicion VARCHAR,
> velocidad REAL,
> altura REAL,
> fecha TIMESTAMP WITHOUT TIME ZONE,
> grados REAL,
> satelites INTEGER,
> CONSTRAINT positions_pkey PRIMARY KEY(id)
> ) WITHOUT OIDS;
>
> --Crear la tabla hist_positions
> CREATE TABLE public.hist_positions (
> id SERIAL,
> gps_id INTEGER,
> posicion VARCHAR,
> velocidad REAL,
> altura REAL,
> fecha TIMESTAMP WITHOUT TIME ZONE,
> grados REAL,
> satelites INTEGER,
> CONSTRAINT hist_positions_pkey PRIMARY KEY(id)
> ) WITHOUT OIDS;
>
> --Función para guardar en positions
> CREATE OR REPLACE FUNCTION public.fun_guardar_posicion (
> gps_id_ integer,
> posicion_ varchar,
> velocidad_ real,
> altura_ real,
> fecha_ timestamp,
> grados_ real,
> satelites_ integer
> )
> RETURNS integer AS
> $body$
> DECLARE
> retorno INTEGER;
> BEGIN
> UPDATE positions
> SET
> posicion = posicion_,
> velocidad = velocidad_,
> altura = altura_,
> fecha = fecha_,
> grados = grados_,
> satelites = satelites_
> WHERE gps_id = gps_id_
> RETURNING id INTO retorno;
>
> IF (retorno IS NULL) THEN
> INSERT INTO positions
> (
> gps_id,
> posicion,
> velocidad,
> altura,
> fecha,
> grados,
> satelites
> )
> VALUES (
> gps_id_,
> posicion_,
> velocidad_,
> altura_,
> fecha_,
> grados_,
> satelites_
> )RETURNING id INTO retorno;
> END IF;
>
> RETURN retorno;
> END;
> $body$
> LANGUAGE 'plpgsql';
>
> --Función ejecutada por el trigger para guardar en el historial de
> positions
> CREATE OR REPLACE FUNCTION public.fun_trg_hist_positions (
> )
> RETURNS trigger AS
> $body$
> DECLARE
> BEGIN
> INSERT INTO hist_positions
> (
> gps_id,
> posicion,
> velocidad,
> altura,
> fecha,
> grados,
> satelites
> )
> VALUES (
> NEW.gps_id,
> NEW.posicion,
> NEW.velocidad,
> NEW.altura,
> NEW.fecha,
> NEW.grados,
> NEW.satelites
> );
>
> RETURN NULL;
> END;
> $body$
> LANGUAGE 'plpgsql';
>
> --Trigger utilizado
> CREATE TRIGGER trg_hist_positions AFTER INSERT OR UPDATE
> ON public.positions FOR EACH ROW
> EXECUTE PROCEDURE public.fun_trg_hist_positions();
>
> Saludos a todos.
> ________________________________________
> From: pgsql-es-ayuda-owner(at)postgresql(dot)org [
> pgsql-es-ayuda-owner(at)postgresql(dot)org] On Behalf Of Jorge Toro [
> jolthgs(at)gmail(dot)com]
> Sent: Monday, July 09, 2012 9:43 AM
> To: Foro Postgres
> Subject: Re: [pgsql-es-ayuda] Ayuda con inquietud
>
> Hola Lazáro, gracias por responder.
>
> Sí gps.id<http://gps.id> es la referencia a una tabla "gps" que se usa
> para registrar cada uno de los GPS que se registran para ser aceptados por
> el servidor.
> Esta tabla "gps" tiene datos como: id, name, type, active. de cada uno de
> los GPS. Y la tabla "position" se encarga de almacenar los reportes
> (históricos) hechos por cada GPS.
>
> Y lo que quiero lograr es, tener a la mano siempre que consulte un
> dispositivo, toda la última información de dicho dispositivo que se
> encuentre en la tabla "position".
>
> Pero no quiero realizar una consulta directa a la tabla "position" porque
> son alrededor de 1000 GPS reportando cada minuto y esta tabla "position"
> tenderá a ser demasiado grande con el pasar del tiempo.
>
>
> Por tu ayuda muchas gracias,
>
>
> Jorge Alonso Toro
> Ing. Teleinformático.
>
> http://jolthgs.wordpress.com/
> www.devmicrosystem.com<http://www.devmicrosystem.com>
> --------------------------------------------------------------
> Powered By Debian.
> Developer Bullix GNU/Linux.
> --------------------------------------------------------------
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iD8DBQBIWWH6q7mzdgTzI5ARAkX5AJ9TR6hL2ocLMOUDRfhts8DlVl+jpwCeNw5x
> p4+4FNUHPDUx1lU9F8WSKCA=
> =zRhQ
> -----END PGP SIGNATURE-----
> Este correo esta protegido bajo los términos de la Licencia
> Atribución-Compartir Obras Derivadas Igual a 2.5 Colombia de Creative
> Commons. Observé la licencia visitando este sitio
> http://creativecommons.org/licenses/by-sa/2.5/co/.
>
>
> El 9 de julio de 2012 09:17, Lazáro Rubén García Martínez <
> lgarciam(at)vnz(dot)uci(dot)cu<mailto:lgarciam(at)vnz(dot)uci(dot)cu>> escribió:
> La columna gps_id es única
>
>
> ________________________________
> Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE
> ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
> http://www.antiterroristas.cu
> http://justiciaparaloscinco.wordpress.com
>
> Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE
> ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
> http://www.antiterroristas.cu
> http://justiciaparaloscinco.wordpress.com
> -
> Enviado a la lista de correo pgsql-es-ayuda (pgsql-es-ayuda(at)postgresql(dot)org
> )
> Para cambiar tu suscripción:
> http://www.postgresql.org/mailpref/pgsql-es-ayuda
>
>
>

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Guillermo Villanueva 2012-07-09 17:06:37 Comportamiento correcto del "not in" ?
Previous Message Alejandro Carrillo 2012-07-09 16:12:11 Re: Ayuda con inquietud