I am trying to use a virtual layer in QGIS (3.30) to add a region field to a suburb layer, by locating the centroid of the suburb within a region. The general approach is taken from: Spatial join based on polygon centroid in QGIS
Both the suburb layer and the region layer are of polygon type, in GeoPackage format, with a spatial index added using Vector->Create Spatial Index... I confirmed that the spatial indexes existed, using ogrinfo eg: ogrinfo -sql "SELECT HasSpatialIndex('suburbs', 'geom')" suburbs.gpkg
The suburb layer has around 4500 records, the region layer has 100 records. This doesn't seem excessive.
However, the virtual layer is taking around 7 minutes to create, and much longer (30min+ ?) to bring up the attribute table (F6).
Query for the virtual layer is as follows - the region details/geometries are in the layer ABSStatisticalAreasLevel3_ :
select s.*, l3.SA3_NAME11
from Suburbs_ s
join ABSStatisticalAreasLevel3_ l3 on ST_CONTAINS(l3.geometry, ST_CENTROID(s.geometry))
There are other questions around slow performance of virtual layers eg:
However, I haven't found any useful answers, other than possibly to try PostGIS.
Are there any practical ways of speeding up the performance of the virtual layer in this instance?
[Note that I can produce the same outcome by:
- creating a Centroid layer from the suburb layer using the Centroid tool from processing toolbox
- adding a field using say
array_first(overlay_within('ABSStatisticalAreasLevel3_',"SA3_NAME11")) - joining this field back to the original suburb layer via a unique id
However, it is considerably more clunky. Hence the desire to get virtual layers working.]

ST_Intersectsrather thanST_Contains? – PyMapr Sep 18 '23 at 05:24_search_frame_in your query! This lists.osgeo.org/pipermail/qgis-developer/2021-May/063582.html helped me a lot.. – Kay Oct 03 '23 at 13:30