我有一个非常慢(0.4 秒)的 mysql 搜索,并且似乎正在扫描所有表行而不使用索引。我尝试将查询限制为仅查找 10 英里边界框中的记录,但查询的速度与似乎没有使用边界框的速度相同。
我已经简化了下面的查询以删除不必要的东西:
SELECT lists.* ,
round( sqrt( ( ( (lists.latitude - '53.72153589619174') * (lists.latitude - '53.72153589619174') ) * 69.1 * 69.1 ) + ( (lists.longitude - '-2.4863433837890625') * (adverts.longitude - '-2.4863433837890625') * 53 * 53 ) ), 1 ) as distance
FROM lists
WHERE (lists.type_id = '3') AND (advert_type = '1') AND (lists.status = 1) AND (lists.approved = 1)
AND (lists.latitude BETWEEN '53.5769627602' AND '53.8661090322' )
AND (lists.longitude BETWEEN '-2.73067484548' AND '-2.2420119221' )
HAVING distance < 10
ORDER BY distance ASC LIMIT 0,20
这是解释:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE lists ref type_breed,searchit,breedsearch,status-approved, typesearch, searchitprice,status_approved_updated, latlon, typesearch-price searchit 4 const,const,const,const 11867 Using where; Using filesort
这是表和索引:
CREATE TABLE `lists` (
`advert_id` int(10) NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`type_id` tinyint(1) NOT NULL,
`breed_id` int(10) NOT NULL,
`advert_type` tinyint(1) NOT NULL,
`headline` varchar(50) NOT NULL,
`description` text NOT NULL,
`price` int(4) NOT NULL,
`postcode` varchar(7) NOT NULL,
`town` varchar(60) NOT NULL,
`county` varchar(60) NOT NULL,
`latitude` float NOT NULL,
`longitude` float NOT NULL,
`telephone1` varchar(15) NOT NULL,
`telephone2` varchar(15) NOT NULL,
`email` varchar(80) NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0',
`approved` tinyint(1) NOT NULL DEFAULT '0',
`date_created` datetime NOT NULL,
`date_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`expiry_date` datetime NOT NULL,
PRIMARY KEY (`advert_id`),
KEY `price` (`price`),
KEY `user` (`user_id`),
KEY `type_breed` (`type_id`,`breed_id`),
KEY `headline_keywords` (`headline`),
KEY `date_updated` (`date_updated`),
KEY `advert_index` (`price`,`status`,`approved`,`date_updated`),
KEY `searchit` (`type_id`,`advert_type`,`status`,`approved`),
KEY `breedsearch` (`type_id`,`breed_id`,`status`,`approved`),
KEY `status-approved` (`status`,`approved`),
KEY `typesearch` (`type_id`,`status`,`approved`,`date_updated`),
KEY `searchitprice` (`type_id`,`advert_type`,`status`,`approved`,`price`),
KEY `status_approved_updated` (`status`,`approved`,`date_updated`),
KEY `latlon` (`latitude`,`longitude`),
KEY `typesearch-price` (`type_id`,`status`,`approved`,`price`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8