views:

34

answers:

2

I can't seem to find much documentation on this. What's the simplest way to create a database/table on postgres supporting a query like this SELECT * FROM table WHERE distance(POINT(0,0), table.location) <= 1000m; Where POINT(0,0) and table.location should be latitude/longitude pair, and 1000m is 1000 meters. And how should I go about indexing that table? Thanks.

A: 

Have you looked into Postgis and contrib/earthdistance?

http://www.postgis.org/

http://www.postgresql.org/docs/9.0/interactive/earthdistance.html

Denis
A: 

PostgreSQL support indexes on expressions and also partial indexes, you can probably mix them.

This is just a random guess, I don't know if it works, but give it a try:

CREATE INDEX foobar ON table (distance(POINT(0,0), location))
 WHERE distance(POINT(0,0), location) <= 1000;

http://www.postgresql.org/docs/9.0/interactive/indexes-expressional.html

http://www.postgresql.org/docs/9.0/interactive/indexes-partial.html

m.m.