What is your version of PostgreSQL: 8.0, 8.1, etc.? If you have a “high version”, you can try including lat and lon columns as a unique point type column. Like this:
create table MyTable ( ... lat integer, lon integer, coor point, ... ); insert MyTable (..., lat, lon, coor, ...) values (..., lat1, lon1, '(lat1, lon1)', ...) ...
And create the indices needed for testing:
create index MyTable_lat on MyTable (lat); create index MyTable_lon on MyTable (lon); create index MyTable_coor on MyTable using gist (coor);
Now you can check which type of request is faster:
explain analyze select * from MyTable where lat < :maxlat and lat > :minlat and lon < :maxlon and lon > :minlon
Or:
explain analyze select * from MyTable where coor <@ box '((:minlat,:minlon),(:maxlat,:maxlon))'
I tested PostgreSQL 9 (with 20,000 entries), and the second is faster.
source share