1

I have two tables in PostgreSQL 13:

  • hexbin table with geom field (red)
  • sites table with geom field (blue)

enter image description here

I want to return a table with the hexbin geometries and a total count of where the sites fall within the hexbin (counting site centers in hexbins).

The following code does appear to return me what I require:

SELECT hexbin.gid, hexbin.geom, count(*) AS site_total
    FROM sites, hexbin
    WHERE ST_intersects(hexbin.geom, (ST_PointOnSurface(sites.geom)))
GROUP BY hexbin.gid

enter image description here

Can this approach be improved?

From looking at other samples and my previous experience with SQL Server Spatial, should I be incorporating a join to improve performance?

2
  • 1
    No, technically this is the way, and it's a JOIN already. It's good practice to use the verbose JOIN syntax, though. Maybe use COUNT(sites.*).
    – geozelot
    Commented Jan 23, 2021 at 7:22
  • 1
    Do you have spatial indices for both table? You can use EXPLANE to check the execution of your query (e.g. indices are used). It may be faster if you make a fast decision using bounding boxes (e. g. WHERE hexbin.geom && sites.geom and ST_Intersects(....)
    – Zoltan
    Commented Jan 23, 2021 at 7:34

1 Answer 1

2

You have not provided test data to check the different options for optimizing your query and yet, splitting your query into simple subqueries can lead to a 2-n times optimization, so check my assumption on your data and for this run the query:

WITH
    tbla AS (SELECT ST_PointOnSurface(geom) geom FROM sites),
    tblb AS (SELECT a.gid, (a.geom) geom, count(*) AS sites_total FROM hexbin a
        WHERE EXISTS (SELECT 1 FROM tbla b WHERE ST_Intersects(a.geom, b.geom)) GROUP BY a.gid)
    SELECT a.gid, a.geom, SUM(a.sites_total) AS sum_sites FROM tbla b, tblb a 
    WHERE ST_Intersects(a.geom, (b.geom)) GROUP BY a.gid, a.geom

Check your result.

There's always room for optimization as long as the query execution speed doesn't turn to "0".

4
  • Cyril, this looks great - thank you. Testing now.
    – jakc
    Commented Jan 25, 2021 at 2:06
  • Seems to work well on small sets of data. But times out with larger datasets (Hexbins - >50k records), whereas the original SQL still parses in less than a second. Will break your query apart and investigate further.
    – jakc
    Commented Jan 25, 2021 at 2:21
  • It may still need to look for alternative approaches, my approach optimizes and narrows the amount of geodata, at the code level, namely: 1) converts polygons to points, getting rid of unnecessary coordinates; 2) gets rid of unnecessary hexagons, if any, and 3) selects hexagons and summarizes the abbonnet count...also possible to play with the technical capabilities of the PC, but only cautiously... Translated with www.DeepL.com/Translator (free version) Commented Jan 25, 2021 at 16:59
  • With large sets of geodata, everyone gets a timeout...:-) Commented Jan 25, 2021 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.