1

I have a table containing a sample set of ~40k rows, with each row containing a Point (longitude and latitude). I'm using MySQL's spatial extension to find which Points exist within a boundary.

If I search within a boundary where I know there will be few results, the index is used as it should be. However, my test dataset mainly consists of locations around the UK (and the larger, live dataset is likely to be similar in percentage terms). When I search within this bounding box, the index doesn't get used and instead a full table scan is performed:

id  select_type     table   type    possible_keys      key  key_len     ref     rows    Extra 
1   SIMPLE          ep      ALL     PRIMARY,location   NULL NULL        NULL    37221   Using where

So it seems as though MySQL is deciding to ignore the index when the number of rows it expects to find is close to the total row count.

Is this the case, and if so, can I force the index to be used or otherwise ensure that a full table scan is avoided?

2
  • 1
    Post your query. What's probable is that you're going to get a lot of results back. In that case, traversing the index to return lots of results is avoided, since it's not needed.
    – N.B.
    Mar 29, 2012 at 11:55
  • Also if you can post table_structure would be good too. Mar 29, 2012 at 12:15

1 Answer 1

0

N.B was correct, simply adjusting the LIMIT downwards means the index is used. Obvious really.

1
  • Don't be fooled by the LIMIT clause, it doesn't mean anything is used. LIMIT OFFSET will fetch the results and then discard OFFSET number of items. If you are using MyISAM tables, google for "late row lookups" and adjust your query accordingly.
    – N.B.
    Mar 29, 2012 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.