SpatialIndex
Not logged in

back

Spatial Index: best practices

The latest versions of SpatiaLite (starting since version 3.0.0) introduced a new mechanism supporting the Spatial Index and based on VirtualSpatialIndex.

Please note well: always using this latest interface is the suggested best practice in order to query any Spatial Index. Just a short rationale accounting for this:
SELECT * 
FROM com2011 AS c, prov2011 AS p
WHERE ST_CoveredBy(c.geometry, p.geometry) = 1 
  AND nome_pro = 'AREZZO' AND c.ROWID IN (
    SELECT ROWID 
    FROM SpatialIndex
    WHERE f_table_name = 'com2011' 
        AND search_frame = p.geometry);
The above SQL snippet shows how to use an inner sub-query so to take full profit from a Spatial Index.

Spatial Index and ATTACH DATABASE

SQLite allows to attach more than a single DB-file to the current connection.
Even in this case you can still use the VirtualSpatialIndex interface:
ATTACH DATABASE secondary.sqlite AS a

SELECT * 
FROM a.com2011 AS c, prov2011 AS p
WHERE ST_CoveredBy(c.geometry, p.geometry) = 1 
  AND nome_pro = 'AREZZO' AND c.ROWID IN (
    SELECT ROWID 
    FROM SpatialIndex
    WHERE f_table_name = 'DB=a.com2011' 
        AND search_frame = p.geometry
)
For this special case, you can still use the SpatialIndex interface, but with a slightly different syntax:


back