The MyISAM table has 4.5m rows. Query with just LIMIT returns fast; query with just ORDER BY returns fast; combining both it never finishes. I've also tried USE INDEX(indexedColumn, Latitude, Longitude), but it doens't help.
SELECT * FROM stuff WHERE (Latitude BETWEEN '29.187190582784076' AND '29.761053992852936' AND Longitude BETWEEN '-101.0597705588786' AND '-99.7414111838786') LIMIT 100;
89 rows; returns in 0.71 seconds
SELECT * FROM stuff WHERE (Latitude BETWEEN '29.187190582784076' AND '29.761053992852936' AND Longitude BETWEEN '-101.0597705588786' AND '-99.7414111838786') ORDER BY indexedColumn DESC;
89 rows, returns in 0.84 seconds
SELECT * FROM stuff WHERE (Latitude BETWEEN '29.187190582784076' AND '29.761053992852936' AND Longitude BETWEEN '-101.0597705588786' AND '-99.7414111838786') ORDER BY indexedColumn DESC LIMIT 100;
hasnt returned after several minutes
EXPLAIN ORDER BY with LIMIT
+----+-------------+-------+-------+--------------------------+--------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+--------------------------+--------------+---------+------+------+-------------+
| 1 | SIMPLE | Stuff | index | Latitude,Longitude | indexedColumn| 5 | NULL | 9643 | Using where |
+----+-------------+-------+-------+--------------------------+--------------+---------+------+------+-------------+
LIMIT ONLY
+----+-------------+-------+-------+--------------------+----------+---------+------+-------+-----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+--------------------+----------+---------+------+-------+-----------------------------------------------+
| 1 | SIMPLE | stuff | range | Latitude,Longitude | Latitude | 30 | NULL | 81158 | Using index condition; Using where; Using MRR |
+----+-------------+-------+-------+--------------------+----------+---------+------+-------+-----------------------------------------------+
ORDER BY ONLY
+----+-------------+-------+-------+--------------------+----------+---------+------+-------+---------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+--------------------+----------+---------+------+-------+---------------------------------------------------------------+
| 1 | SIMPLE | stuff | range | Latitude,Longitude | Latitude | 30 | NULL | 81158 | Using index condition; Using where; Using MRR; Using filesort |
+----+-------------+-------+-------+--------------------+----------+---------+------+-------+---------------------------------------------------------------+
I need MyISAM for full text search, and AWS RDS does not have the 5.6.4. I didn't try this with InnoDB.
I have also tried this on a MariaDB server and the outcome is the same. I increased the server memory size and didn't help.
I've also tried using spatial geometry instead of lat/lon doubles, and the outcome was the same.
Also tried this:
SELECT * FROM stuff ORDER BY indexedColumn DESC LIMIT 100
100 rows; 0.06 sec
I used an AWS RDS with 4gb memory and increased key_buffer_size to 2gb. The indexes on table are 600mb. Perhaps there are other configurations I'm unaware of that aren't taking advantage of the memory, since RDS is mainly configured for InnoDB.
-------------EDIT---------------------
I created an 8GB AWS RDS MySQL instance with the data. I used ST_CONTAINS on spatial indexed point which returns in 1.1 second (which works for me).
I am still interested in knowing why the other queries have problems though. I feel like it's probably a configuration issue with limited RAM. I've done hundreds of queries like this and never run into a problem like this. Seems like a random fluke.
I'll try converting the MyISAM table to InnoDB to see if it matters.