Query structure:
In general, querying a table for true radius lookups is best executed using ST_DWithin
, i.e.:
SELECT *
FROM <your_table>
WHERE ST_DWithin(<geom_colum>, <ref_geom>, <distance>);
where <geom_column>
refers to the tables geometry column, <ref_geom>
the e.g. point geometry in question, and <distance>
the radius in units of the CRS (see Notes on CRS).
With proper indexes, this should be orders of magnitude faster than other query designs.
If true radius is not of concern, a direct bbox search should again be substantially faster on very large tables, i.e.
SELECT *
FROM <your_table>
WHERE <geom_colum> && ST_Expand(<ref_geom>, <distance>);
This will, of course, find geometries in the rectangular bbox around the <ref_geometry>
, and in the case of an expanded point, the square with the <distance> * 2
dimensions.
Note that combining both does not improve execution time in my experience; ST_DWithin
already implicitly executes the same bbox filter.
Notes on CRS:
Naturally, of substantial importance is the underlying CRS. ST_DWithin
(as well as e.g. ST_Expand
) will assume <distance>
to be in the units of the data's CRS; if your data is in a geographic projection (e.g. EPSG:4326), units are degrees...and as such useless.
You have two options (pros and cons are from the top of my head):
I'm not in the US, the TIGER data and geocoding service are not much of interest to me, and I don´t know the data's details, but I expect the CRS to be EPSG:4326 in order to guarantee nationwide consistency?
If that is the case, I would recommend to use a cast to geography and accept the larger latency in execution, e.g.
SELECT *
FROM <your_table>
WHERE ST_DWithin(<geom_colum>::geography, <ref_geom>::geography, <distance_in_mi> * 1609.34);
or, for completeness, the same with a bbox filter (note the use of _ST_Expand
, with a leading _
; this 'meta-function' is not exactly encouraged to use, but works on geography type)
SELECT *
FROM <your_table>
WHERE <geom_colum>::geography && _ST_Expand(<ref_geom>::geography, <distance_in_mi> * 1609.34);
Note: you can set the use_spheroid
parameter of ST_DWithin
to false
to increase performance (calculations will be based on a sphere instead of a spheroid; less precision, faster check).
If you intend to run this over and over on a nationwide dataset, consider the extra time and disk space and add a geography column with an own index.
TIGER data (not my expertise):
As to why your tabblock
table is empty: did you import the data?
I do hope this actually is you issue; getting geometries from a present table within a radius. About the presence of that table: as far as I got it from a very quick glance at the docs, the TIGER geocoder works on manually pre-loaded data!?