Lets say I have a function, POINT_IN_SHAPE('shape_name', x, y)
that is an expensive-ish call, so I would want to reduce the amount of times it would be called. One way to prevent calling the function is to draw a bounding box around the shape. For example, if a point's x is greater than the maximum x value in the shape, then it's not inside.
if(x > max_X) return outside;
With this in mind, would adding that condition to my WHERE clause speed up the query by reducing the amount of rows that POINT_IN_SHAPE()
needs to run on? Let's say there in an index on x, and POINT_IN_SHAPE()
does not do a bounding box check.
select * from points where x <= max_X and POINT_IN_SHAPE(...);
vs
select * from points where POINT_IN_SHAPE(...);
I'm hoping the conditions reduce the set of rows for other conditions, but I'm not sure how SQL works. I'm also hoping that it'll be smart enough to reduce rows first using the cheaper condition.
I don't really care if the ordering of the conditions matters, I'm just wondering if one condition would reduce the "search space" for another condition. From the comments, answers, links to other threads, and a bit of testing, it looks like the conditions do reduce the search space for other conditions in the where clause.