educative.io

Updated GridID fetching SQL query

In the Yelp article, author suggests that the new updated SQL select query with geogrids is:

Select * from Places where Latitude between X-D and X+D and Longitude between Y-D and Y+D and GridID in (GridID, GridID1, GridID2, ..., GridID8)

But wouldn’t this run into exactly the same problems pointed out earlier with intersecting the results of two indices taking too long (i.e. the range queries on lat and long)

2 Likes

I have the same question. But I am guessing that it depends on query execution plan. Wouldn’t the execution plan be like: search for records with specified grid ids and then apply the lat, long filters? Also we can modify the query to use sub-query Like Select * from (Filter table with grid ids) x where Latitude between X-D and X+D and Longitude between Y-D and Y+D. I might be incorrect.

1 Like

I have the same question. @Design_Gurus

Seem like author never answer user’s question.

how would you find Grid1…Grid8 in his approach?

1 Like

Here I think because we have created index on GridIDs, database smartly first finds the GridIDs of interest in the DB and then look for lat/long range which will result in much less search area.

1 Like

That means you will scan the whole table on lat,long to find GridIds of interest?