I am using 'explain' to view why my query is hitting every single row in my database, and I do not understand why it is doing so.
Could someone take a look and give me a hint what I am missing?
My database is MyISAM, and I am using Mysql 5.1, PHP5
Here is my table:
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user_id` bigint(20) NOT NULL auto_increment,
`name` varchar(40) default NULL,
`city` varchar(90) default NULL,
`latitude` float NOT NULL default '0',
`longitude` float NOT NULL default '0',
PRIMARY KEY (`user_id`),
UNIQUE KEY `name` (`name`),
KEY `Radius Search` (`latitude`,`longitude`),
KEY `Radius 2` (`longitude`,`latitude`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=38666 ;
Here is my query:
$query =
"SELECT
name, city
FROM
users
WHERE
(
(69.1 * (latitude - " . $user->latitude . ")) *
(69.1 * (latitude - " . $user->latitude . "))
) + (
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) *
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))
) < " . pow($radius, 2) . "
ORDER BY
(
(69.1 * (latitude - " . $user->latitude . ")) *
(69.1 * (latitude - " . $user->latitude . "))
) + (
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) *
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))
) ASC";
And finally, my explain...
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE users ALL NULL NULL NULL NULL 38665 Using where; Using filesort