Skip to content

Efficient spatial join? #303

Answered by Maxxen
nshiab asked this question in Q&A
Apr 16, 2024 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

Hi! I think the better way to do a spatial join is to use a proper inner join with a spatial predicate as join condition, instead of doing a cross product and then filtering (which is going to have to create an exponential amount of geometries just to filter most of them out).

e.g. instead of doing this

SELECT * FROM t1 CROSS JOIN t2 WHERE st_intersects(t1.geom, t2.geom);

Do this:

SELECT * FROM t1 JOIN t2 ON st_intersects(t1.geom, t2.geom)

Inner joins like this can also be optimized further by DuckDB spatial as it will attempt to rewrite the query to first join on only the geometries who's cached bounding boxes intersect, which avoids doing the actual costly intersection comparison for ge…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@nshiab
Comment options

@nshiab
Comment options

@johngcrowley
Comment options

@johngcrowley
Comment options

Answer selected by nshiab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants