Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to return a list of geometries that satisfy two ST_Intersects conditions, and am running into a performance issue that I hope is remediable. My query looks like this:

SELECT DISTINCT a.id
FROM layer1 a, layer2 b, layer3 c
WHERE (ST_Intersects(a.GEOMETRY, b.GEOMETRY) AND b.ROWID IN (SELECT ROWID FROM SpatialIndex WHERE f_table_name='layer2' AND search_frame=a.GEOMETRY))
OR (ST_Intersects(a.GEOMETRY, c.GEOMETRY) AND c.ROWID IN (SELECT ROWID FROM SpatialIndex WHERE f_table_name='layer3' AND search_frame=a.GEOMETRY))

Individually, both ST_Intersects operations are quite fast and return in a second or two. However, when I combine both conditions in the WHERE clause, the whole thing appears to take exponentially longer, as if it is doing a table scan.

Is there an efficient way to write this query?

share|improve this question
How about a UNION of two queries in place of the OR. This would probably give some duplicate rows, when a intersects b, and a intersects c. But it might be faster, since you'd avoid running the second part of the OR condition for each row of the first part of the WHERE clause. – Micha Jul 17 '12 at 19:41

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.