pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Query with LIMIT but as random result set?
8+ messages / 5 participants
[nested] [flat]

* Query with LIMIT but as random result set?
@ 2013-01-08 15:20  Stefan Keller <sfkeller@gmail.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Stefan Keller @ 2013-01-08 15:20 UTC (permalink / raw)
  To: pgsql-general; PostGIS Users Discussion <postgis-users@postgis.refractions.net>

Hi

I have a query like this

  SELECT ST_AsText(way) geom, name AS label
  FROM osm_point
  LIMIT 10;

When I repeatedly do this, the result set will be always the same.
I have observed this only empirically and I know that the ordering of
the result set is undefined without ORDER BY.
There are two indexes involved, one geospatial for way and one for name.

My question is: Does someone have an idea on how to randomize the
result set on every consecutive query?
And as an option the (limited) resultset should be spatially
distributed (not clustered).

Yours, Stefan


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Query with LIMIT but as random result set?
@ 2013-01-08 15:27  Adrian Klaver <adrian.klaver@gmail.com>
  parent: Stefan Keller <sfkeller@gmail.com>
  1 sibling, 0 replies; 8+ messages in thread

From: Adrian Klaver @ 2013-01-08 15:27 UTC (permalink / raw)
  To: Stefan Keller <sfkeller@gmail.com>; +Cc: pgsql-general; PostGIS Users Discussion <postgis-users@postgis.refractions.net>

On 01/08/2013 07:20 AM, Stefan Keller wrote:
> Hi
>
> I have a query like this
>
>    SELECT ST_AsText(way) geom, name AS label
>    FROM osm_point
>    LIMIT 10;
>
> When I repeatedly do this, the result set will be always the same.
> I have observed this only empirically and I know that the ordering of
> the result set is undefined without ORDER BY.
> There are two indexes involved, one geospatial for way and one for name.
>
> My question is: Does someone have an idea on how to randomize the
> result set on every consecutive query?
> And as an option the (limited) resultset should be spatially
> distributed (not clustered).


SELECT ST_AsText(way) geom, name AS label
     FROM osm_point ORDER BY random()
     LIMIT 10;

>
> Yours, Stefan
>
>


-- 
Adrian Klaver
adrian.klaver@gmail.com


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [postgis-users] Query with LIMIT but as random result set?
@ 2013-01-08 17:24  Stefan Keller <sfkeller@gmail.com>
  parent: Stefan Keller <sfkeller@gmail.com>
  1 sibling, 0 replies; 8+ messages in thread

From: Stefan Keller @ 2013-01-08 17:24 UTC (permalink / raw)
  To: PostGIS Users Discussion <postgis-users@lists.osgeo.org>; pgsql-general

Hi,

Thanks a lot to Adrian, Nicolas and Brooks.
"... ORDER BY random() LIMIT 10;" works ok.

But with the following option it gets more tricky assume:
> And as an option the (limited) resultset should be spatially
> distributed (not clustered).

I'm thinking about some radial spatial distribution function.

Yours, Stefan


2013/1/8 Brooks Kehler <brookskehler@gmail.com>:
> this should work -
>
> order by random() limit 10;
>
>
>
> On Tue, Jan 8, 2013 at 10:20 AM, Stefan Keller <sfkeller@gmail.com> wrote:
>>
>> Hi
>>
>> I have a query like this
>>
>>   SELECT ST_AsText(way) geom, name AS label
>>   FROM osm_point
>>   LIMIT 10;
>>
>> When I repeatedly do this, the result set will be always the same.
>> I have observed this only empirically and I know that the ordering of
>> the result set is undefined without ORDER BY.
>> There are two indexes involved, one geospatial for way and one for name.
>>
>> My question is: Does someone have an idea on how to randomize the
>> result set on every consecutive query?
>> And as an option the (limited) resultset should be spatially
>> distributed (not clustered).
>>
>> Yours, Stefan
>> _______________________________________________
>> postgis-users mailing list
>> postgis-users@lists.osgeo.org
>> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>
>
>
> _______________________________________________
> postgis-users mailing list
> postgis-users@lists.osgeo.org
> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [postgis-users] Query with LIMIT but as random result set?
@ 2013-01-10 20:28  Kevin Grittner <kgrittn@mail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Kevin Grittner @ 2013-01-10 20:28 UTC (permalink / raw)
  To: Stefan Keller <sfkeller@gmail.com>; PostGIS Users Discussion <postgis-users@lists.osgeo.org>; pgsql-general

Stefan Keller wrote:

> "... ORDER BY random() LIMIT 10;" works ok.
> 
> But with the following option it gets more tricky assume:
>> And as an option the (limited) resultset should be spatially
>> distributed (not clustered).
> 
> I'm thinking about some radial spatial distribution function.

So, you explicitly *don't* want a random selection?  By "spatially
distributed" you mean that if you have already chosen one
particular location, other locations which are close to it should
be less probable (or impossible) to include in the limited result
set? How would you define the desired result? The one with the
highest best solution to the "traveling salesman" problem?

-Kevin


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [postgis-users] Query with LIMIT but as random result set?
@ 2013-01-10 22:36  Stefan Keller <sfkeller@gmail.com>
  parent: Kevin Grittner <kgrittn@mail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Stefan Keller @ 2013-01-10 22:36 UTC (permalink / raw)
  To: Kevin Grittner <kgrittn@mail.com>; +Cc: PostGIS Users Discussion <postgis-users@lists.osgeo.org>; pgsql-general

Hi Kevin

No; I'm thinking about some query (or function) that selects random
points (POIs) with certain characteristics like decreasing density. I
didn't find much theory about how to *create* such random points.

There seems to be more literature and implementation about measuring
geographic distribution (like in ArcGIS http://bit.ly/13lTFj9 ).

Under "radial distribution function" I understand a function which
describes how density varies depending on the distance from a
reference point (= the user).

Yours, Stefan


2013/1/10 Kevin Grittner <kgrittn@mail.com>:
> Stefan Keller wrote:
>
>> "... ORDER BY random() LIMIT 10;" works ok.
>>
>> But with the following option it gets more tricky assume:
>>> And as an option the (limited) resultset should be spatially
>>> distributed (not clustered).
>>
>> I'm thinking about some radial spatial distribution function.
>
> So, you explicitly *don't* want a random selection?  By "spatially
> distributed" you mean that if you have already chosen one
> particular location, other locations which are close to it should
> be less probable (or impossible) to include in the limited result
> set? How would you define the desired result? The one with the
> highest best solution to the "traveling salesman" problem?
>
> -Kevin


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [postgis-users] Query with LIMIT but as random result set?
@ 2013-01-11 17:31  Gavin Flower <GavinFlower@archidevsys.co.nz>
  parent: Stefan Keller <sfkeller@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Gavin Flower @ 2013-01-11 17:31 UTC (permalink / raw)
  To: Stefan Keller <sfkeller@gmail.com>; +Cc: Kevin Grittner <kgrittn@mail.com>; PostGIS Users Discussion <postgis-users@lists.osgeo.org>; pgsql-general

Hi Stefan,

Please do not to post!  :-)
Here, we post replies at the end, so people can see the context before 
the new stuff.


On 11/01/13 11:36, Stefan Keller wrote:
> Hi Kevin
>
> No; I'm thinking about some query (or function) that selects random
> points (POIs) with certain characteristics like decreasing density. I
> didn't find much theory about how to *create* such random points.
>
> There seems to be more literature and implementation about measuring
> geographic distribution (like in ArcGIS http://bit.ly/13lTFj9 ).
>
> Under "radial distribution function" I understand a function which
> describes how density varies depending on the distance from a
> reference point (= the user).
>
> Yours, Stefan
>
>
> 2013/1/10 Kevin Grittner <kgrittn@mail.com>:
>> Stefan Keller wrote:
>>
>>> "... ORDER BY random() LIMIT 10;" works ok.
>>>
>>> But with the following option it gets more tricky assume:
>>>> And as an option the (limited) resultset should be spatially
>>>> distributed (not clustered).
>>> I'm thinking about some radial spatial distribution function.
>> So, you explicitly *don't* want a random selection?  By "spatially
>> distributed" you mean that if you have already chosen one
>> particular location, other locations which are close to it should
>> be less probable (or impossible) to include in the limited result
>> set? How would you define the desired result? The one with the
>> highest best solution to the "traveling salesman" problem?
>>
>> -Kevin
>
Note that even for an even probability distribution,you have to be careful.

There are at least 2 ways to distribute points in a circle, both are 
valid in different contexts.

(1)  Select (x,y) in a square containing the circle, through away any 
points outside the circle.  This has an even probability density per 
unit area of the circle.

(2) Select (theta, r) at random.  This has a greater density of points 
at the centre reducing lineraly towards the circumference.

I think something like the second fits your situation.

DROP TABLE IF EXISTS ranpoint;

CREATE TABLE ranpoint
(
     id      int,
     theta   float,
     r       float
);

-- theta in radians
-- for radius = 100

INSERT INTO ranpoint
     (id, theta, r)
VALUES
     (generate_series(1, 10), pi() * random(), 100 * random());


TABLE ranpoint;


Cheers,
Gavin

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [postgis-users] Query with LIMIT but as random result set?
@ 2013-01-11 17:45  Bosco Rama <postgres@boscorama.com>
  parent: Gavin Flower <GavinFlower@archidevsys.co.nz>
  0 siblings, 1 reply; 8+ messages in thread

From: Bosco Rama @ 2013-01-11 17:45 UTC (permalink / raw)
  To: PostGIS Users Discussion <postgis-users@lists.osgeo.org>; pgsql-general

On 01/11/13 09:31, Gavin Flower wrote:
> -- theta in radians
> -- for radius = 100
> 
> INSERT INTO ranpoint
>      (id, theta, r)
> VALUES
>      (generate_series(1, 10), pi() * random(), 100 * random());

Shouldn't the value for theta be:
     2 * pi() * random()

Bosco.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [postgis-users] Query with LIMIT but as random result set?
@ 2013-01-11 17:49  Gavin Flower <GavinFlower@archidevsys.co.nz>
  parent: Bosco Rama <postgres@boscorama.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Gavin Flower @ 2013-01-11 17:49 UTC (permalink / raw)
  To: Bosco Rama <postgres@boscorama.com>; +Cc: PostGIS Users Discussion <postgis-users@lists.osgeo.org>; pgsql-general

On 12/01/13 06:45, Bosco Rama wrote:
> On 01/11/13 09:31, Gavin Flower wrote:
>> -- theta in radians
>> -- for radius = 100
>>
>> INSERT INTO ranpoint
>>       (id, theta, r)
>> VALUES
>>       (generate_series(1, 10), pi() * random(), 100 * random());
> Shouldn't the value for theta be:
>       2 * pi() * random()
>
> Bosco.
>
>
Very definitely! :-)

Me bad, as the saying goes...


Cheers,
Gavin

^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2013-01-11 17:49 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-01-08 15:20 Query with LIMIT but as random result set? Stefan Keller <sfkeller@gmail.com>
2013-01-08 15:27 ` Adrian Klaver <adrian.klaver@gmail.com>
2013-01-08 17:24 ` Stefan Keller <sfkeller@gmail.com>
2013-01-10 20:28 Re: [postgis-users] Query with LIMIT but as random result set? Kevin Grittner <kgrittn@mail.com>
2013-01-10 22:36 ` Stefan Keller <sfkeller@gmail.com>
2013-01-11 17:31   ` Gavin Flower <GavinFlower@archidevsys.co.nz>
2013-01-11 17:45     ` Bosco Rama <postgres@boscorama.com>
2013-01-11 17:49       ` Gavin Flower <GavinFlower@archidevsys.co.nz>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox