On Wed, 3 Apr 2024 09:44:55 -0700 (PDT), Jim wrote:
> I am building a table of coordinates and other indicative information
> for set of locations.
>
> Over time this table could become quite large.
>
> The table with contain the latitude and longitude as FLOAT values and
> a spatialite POINT built with the same latitude and longitude.
>
> The table will be indexed on the latitude and longitude and with a
> SpatialIndex on the POINT.
>
> Which is the fastest way to search the table using latitude and
> longitude , the Sqlite index or the SpatialIndex?
>
Hi Jim,
I suppose that to deal with a spatial indexing problem, nothing can
beat the R*Tree, i.e. the SpatialIndex, an algorithm designed
specifically for this problem.
> If the answer is the Sqlite indices, should those indices be the
> latitude and longitude individually or as an combined index of the
> latitude and longitude?
>
Working on indices based on floats seems decidedly very unpromising
to me.
One axis will always have priority over the other, so the result
will be a very inefficient spatial filter.
perhaps it might be more promising to use Hilbert codes, but I
see it as a very theoretical possibility.
In any case, newer versions of SpatiaLite support the HilbertCode()
SQL function, but it will certainly take a lot of extra work to get
something usable.
please see for more details:
https://en.wikipedia.org/wiki/Hilbert_curve
> If the answer is SpatialIndex, what would that query look like?
>
CREATE TABLE my_table (
id INTEGER PRIMARY KEY.
... any other stuff ..);
SELECT AddGeometryColumn('my_table', 'geom', 4326, 'POINT', 'XY');
SELECT CreateSpatialIndex('my_table', 'geom'):
SELECT id [, any_other_stuff]
FROM my_table
WHERE rowid IN (SELECT ROWID FROM SpatialIndex
WHERE f_table_name = 'my_table'
AND search_frame = BuildCircleMBR(X, Y, R));
note: X, Y are the coodinates of your refence POINT
and R is the radius of the circle (beware: if you
work with Longitude ana Latitudes it should be
measured in ANGULAR DEGREES).
if for your needs a reasonable approximation
might be acceptable this trick could speed up
your query by making it unnecessary to call
ST_Distance() or any other Spatial function.
bye Sandro