1

I have a large large table (InnoDB) that essentially has a location name with the location's latitude and longitude. my query is something like

SELECT columns FROM table  
WHERE latitude BETWEEN latMin AND latMax
AND longitude BETWEEN longMin AND longMax
AND location LIKE '%mcdonalds%'

the only index is the primary index for the auto incrementing column.

I would prefer to use InnoDB so FULLTEXT isn't an option

One option I tried was adding indexes to the latitude and longitude, and doing

SELECT id FROM table WHERE 
latitude BETWEEN latMin AND latMax
AND longitude BETWEEN longMin AND longMax

then i do

$stringOfIds = implode(",",$result);
SELECT columns FROM table WHERE id IN ($stringOfIds)
AND location LIKE '%mcdonalds%'

But it's not very scalable because the $result can have thousands of entries and takes too much memory

For now, I have resorted to a full table scan (the first query above) but it takes 0.5-1 seconds per query so any help would be appreciated.

Thanks

Andrew Park
  • 1,489
  • 1
  • 17
  • 26
  • afaik, full text index does not require unique values – Maxim Krizhanovsky Sep 28 '11 at 21:46
  • There's nothing about a fulltext index that requires records by unique. It's just a way of doing more efficient text searching than `like '%blahblah%'` could ever be. A fulltext index cannot be unique in any case. Only a normal index can be made unique. – Marc B Sep 28 '11 at 21:47
  • Ah got it. But I would prefer to use InnoDB and InnoDb doesn't support FULLTEXT, right? – Andrew Park Sep 28 '11 at 21:49

1 Answers1

0

The standard B-TREE index is not well suited to this sort of query. I'd recommend changing your design to use the geography type and then creating a SPATIAL index. You can then use this index with MBRContains to quickly find all the points that lie within your bounding box.

Update: Creating a spatial index requires MyISAM as pointed out in the comments.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Sorry, I don't see the difference between your query and mine besides the column1,column2. Also, InnoDB cannot use Spatial index, right? – Andrew Park Sep 28 '11 at 21:50
  • 1
    @AndrewPark, Annoyingly no. If you want proper GIS support you'll have to switch to postgreSQL. Or use MyISAM, but that only has bounding rectangles. – Johan Sep 28 '11 at 21:58
  • @Andrew Park: Sorry didn't see that you already tried that. And yes you are right that you need to use MyISAM to add a SPATIAL index. I forgot about that. Perhaps you can store the spatial data in a separate table so that your master table can remain as InnoDB. You can use an ID to join the two tables. – Mark Byers Sep 28 '11 at 21:58