Which proximity function provides fastest and make use of spatial index in postGIS?
-
11-03-2021 - |
Question
I am new to PostgreSQL / PostGIS. I am evaluating it to solve a simple algorithm : Try to find all points in a radius (meters). Here is my table :
=> \d+ theuser;
Table "public.theuser"
Column | Type | Modifiers | Storage | Description
----------+------------------------+-----------+----------+-------------
id | bigint | not null | plain |
point | geometry | | main |
Indexes:
"theuser_pkey" PRIMARY KEY, btree (id)
"point_index" gist (point)
Referenced by:
...
Has OIDs: no
I add a gist index to the point
column , I don't know if it is the correct design.
All 'points' inserted are with SRID=4326
.
It seems there're 2 ways to get nearby points:
ST_Distance , ST_Distance_Sphere . Take 2 for example :
select * from theuser where
ST_distance_sphere(point , ST_GeomFromText('POINT(120.9982 24.788)',4326)) < 100;
I wonder which algorithm make use of the "point_index
" ? If there are millions of points , can both execute very fast ?
Another question , how can I query the SRID of a cell (I searched by found no answer) ?
All I can do is by hibernate-spatial-postgis , getting "com.vividsolutions.jts.geom.Point
" , and get the SRID from the returned point. How do I query it in SQL ? Thanks.
Environment :
=> select version();
version
-----------------------------------------------------------------------------------------------------------
PostgreSQL 8.4.9 on i486-pc-linux-gnu, compiled by GCC gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3, 32-bit
=> SELECT postgis_lib_version();
postgis_lib_version
---------------------
1.4.0
---- updated ----
Thanks @filiprem , I tried this :
=> explain select * from theuser where
ST_distance_sphere(point , ST_GeomFromText('POINT(120.9982 24.788)',4326)) < 100;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Seq Scan on theuser (cost=0.00..1.15 rows=3 width=2644)
Filter: (st_distance_sphere(point, '0101000020E610000080B74082E23F5E407D3F355EBAC93840'::geometry) < 100::double precision)
(2 rows)
How do I know if it makes use of the "point_index" gist (point)
? Will it survive under high data volume searching ?
Solution
I heard once that ST_DWithin is the fastest, actually in the documentation they say that in newer versions ST_DWithin has been tunned up.
Prior to 1.3, ST_Expand was commonly used in conjunction with && and ST_Distance to achieve the same effect and in pre-1.3.4 this function was basically short-hand for that construct. From 1.3.4, ST_DWithin uses a more short-circuit distance function which should make it more efficient than prior versions for larger buffer regions.
Also it uses bounding box comparitions and indexes:
This function call will automatically include a bounding box comparison that will make use of any indexes that are available on the geometries.