I've taken a zip code database and pre-calculated the distances to other zip codes within a certain radius. The database itself is about 2.5GB so not anything extraordinary.
The goal of doing this is to be able to:
select * from zipcode_distances where zipcode_from=92101 and distance < 10;
So far the only index i've defined is:
(zipcode_from, distance)
However, running the query takes about 20 seconds to get the results.
When I remove the "and distance < 10" clause, the results are instantaneous.
Any advice would be appreciated.
Edit:
Here is the create statement:
delimiter $$
CREATE TABLE `zipcode_distances` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`zipcode_from` char(5) COLLATE utf8_bin NOT NULL,
`zipcode_to` char(5) COLLATE utf8_bin NOT NULL,
`distance` double unsigned NOT NULL COMMENT 'stored in miles',
PRIMARY KEY (`id`),
KEY `idx_zip_from_distance` (`zipcode_from`,`distance`)
) ENGINE=MyISAM AUTO_INCREMENT=62548721 DEFAULT CHARSET=utf8 COLLATE=utf8_bin$$
Here is the explain:
explain extended select * from zipcode_distances where zipcode_from=90210 and distance < 10;
Results:
id, select_type, table, possible_keys, key, key_len, ref, rows, filtered, Extra 1, SIMPLE, zipcode_distances, ALL, idx_zip_from_distance, null, null, null, 62548720, 100.00, Using where
Thank you!
SELECT some_column FROM ...for a start.