I am trying to figure out points reside inside an rectangular area (envelope). I am having a little bit difficulty understanding performance implications of using && operator compared to ST_Intersect.
However I think I explained my own question while trying to formulate my question. I submit it anyway in case it would be useful to somebody.
The manual for && says (ironically the page name is geometry_overlaps.html):
&& — Returns TRUE if A's 2D bounding box intersects B's 2D bounding box.
The manual for ST_Intersects says:
Returns TRUE if the Geometries/Geography "spatially intersect in 2D" - (share any portion of space) and FALSE if they don't (they are Disjoint). For geography -- tolerance is 0.00001 meters (so any points that close are considered to intersect)
The manual for ST_Overlaps says:
ST_Overlaps — Returns TRUE if the Geometries share space, are of the same dimension, but are not completely contained by each other.
So, clearly ST_Overlaps
return false if one object is completely inside another. Which is unsuitable for my objective. Meanwhile ST_Intersects
will return true in such cases. For my objective &&
operator does almost same thing since I am using a rectangular bounding box in either case. I am wondering if &&
is the fastest operator for my purpose? It feels like &&
has to do less checks so it must be much more efficient.
Here is an example which is direct copy from &&
documentation
# SELECT tbl1.column1, tbl2.column1, tbl1.column2 && tbl2.column2 AS overlaps
FROM ( VALUES
(1, 'LINESTRING(0 0, 3 3)'::geometry),
(2, 'LINESTRING(0 1, 0 5)'::geometry)) AS tbl1,
( VALUES
(3, 'LINESTRING(1 2, 4 6)'::geometry)) AS tbl2;
column1 | column1 | overlaps
---------+---------+----------
1 | 3 | t
2 | 3 | f
(2 rows)
Below is ST_Intersects
adaptation:
# SELECT tbl1.column1, tbl2.column1, ST_Intersects(tbl1.column2,tbl2.column2) AS overlaps
FROM ( VALUES
(1, 'LINESTRING(0 0, 3 3)'::geometry),
(2, 'LINESTRING(0 1, 0 5)'::geometry)) AS tbl1,
( VALUES
(3, 'LINESTRING(1 2, 4 6)'::geometry)) AS tbl2;
column1 | column1 | overlaps
---------+---------+----------
1 | 3 | f
2 | 3 | f
(2 rows)
Below is a simple graph of how these lines should look like. I just added plotted this to see how bounding box works exactly for lines. For my purposes, the points are always inside the box (while this line example does not really reflect what I am trying to do but good for illustration)