I have a mysql myisam table with 500,000 rows. In this table I have information of different type of places and latitude & longitude coordinates. Depending on the user I would like to select within a certain distance from a point defined by latitude and longitude a certain type of places.
I have a spatial index and a multi column index on latitude, longitude, type. Those indexes on itsel work well if the number of rows within a certain area is not too big.
The problem is that in some cases I need to use a very large radius from a certain point (defined by latitude, longitude coordinates) because there are very few places of the type required. However the problem is that when I search for a certain type, say "x" mysql searches for around 20,000 rows as my radius is large, say "200 km". However in the real world there are only 5 places with type "x" within 200 km from a certain point.
I read somewhere that BTREE and SPATIAL indexes cannot be combined. However I want to work towards a solution where I am able to select those 5 places very quickly based on the input of latitude, longitude and type.
I tried the below 2 approaches:
APPROACH 1 - spatial index:
SELECT * FROM destinations
WHERE MBRWithin(lat_lng_point, GeomFromText('Polygon((49.8413216059 12.8478000082, 48.0426783941 12.8478000082, 48.0426783941 15.5861999918, 49.8413216059 15.5861999918, 49.8413216059 12.8478000082))'))
AND destinations.type = 'x'
APPROACH 2 - multi column index on latitude, longitude, type:
SELECT * FROM destinations FORCE INDEX (lat_long_type_main)
WHERE latitude > 49.7786783941 AND latitude < 51.5773216059
AND longitude > 10.0927907742 AND longitude < 12.9312092258
AND type = 'x'
Approach 1 is still much faster than approach 2, as they take 2 to 5 seconds respectively. Also the number of rows that is scanned (by using explain) is larger with the second approach than with the first approach.
With approach 1 and approach 2 the number of rows in the explain is exactly the number of rows within the specified region by the geocoordinates, discarding the type. I can understand that for approach 1 the type is not in the index, but not for approach 2 I would not expect a large table scan for type as type is in the index.
If I could make an index that would directly return the 5 points using an index on latitude,longitude and type I expect this query to be much faster.
As I have a number of such queries it is very important to speed them up. I will be very thankfull for your help.
ST_Within
instead to gain accuracy. – Jon Bellamy