简体   繁体   中英

MySQL not using possible index - why?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM