Skip site navigation (1) Skip section navigation (2)

Re: extraer parte decimal

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: Juan <smalltalker(dot)marcelo(at)gmail(dot)com>
Cc: Álvaro Hernández <aht(at)nosys(dot)es>, gilberto(dot)castillo <gilberto(dot)castillo(at)etecsa(dot)cu>, Ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: extraer parte decimal
Date: 2011-06-23 17:13:48
Message-ID: 1308849112-sup-3683@alvh.no-ip.org (view raw or flat)
Thread:
Lists: pgsql-es-ayuda
> 2011/6/23 Álvaro Hernández <aht(at)nosys(dot)es>

> > CREATE FUNCTION random_rows(table_name text, n_rnd_rows int, n_rows bigint)
> > RETURNS SETOF RECORD AS $$
> >        DECLARE
> >                curs            refcursor;
> >                ret             record;
> >                rnd_row_pos     int[];
> >        BEGIN
> >                FOR i IN 1..n_rnd_rows LOOP
> >                        rnd_row_pos[i] = floor(random() * n_rows);
> >                END LOOP;
> >
> >                -- Sort the array
> >                SELECT INTO rnd_row_pos array_agg(i) FROM (SELECT * FROM
> > unnest(rnd_row_pos) i ORDER BY i) AS i;
> >
> >                OPEN curs FOR EXECUTE 'SELECT * FROM ' || table_name; --
> > ORDER is not relevant
> >
> >                MOVE ABSOLUTE rnd_row_pos[1] - 1 FROM curs;
> >                FETCH NEXT FROM curs INTO ret;
> >                RETURN NEXT ret;
> >
> >                FOR i IN 2..n_rnd_rows LOOP
> >                        MOVE RELATIVE rnd_row_pos[i] - rnd_row_pos[i - 1] -
> > 1 FROM curs;
> >                        FETCH NEXT FROM curs INTO ret;
> >                        RETURN NEXT ret;
> >                END LOOP;

El problema de esta idea es que cada MOVE y FETCH requieren leer todas
las tuplas intermedias para contarlas.  Capturar una tupla que está
hacia el final de la tabla es muy costoso.

-- 
Álvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>

In response to

Responses

pgsql-es-ayuda by date

Next:From: Álvaro Hernández TortosaDate: 2011-06-23 17:19:46
Subject: Re: extraer parte decimal
Previous:From: Sebastián VillalbaDate: 2011-06-23 17:12:02
Subject: "ERROR: Memoria agotada" - "ERROR: invalid memory alloc request size"

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group