MySQL Query执行时间太长

时间:2013-07-19 23:18:32

标签: mysql sql database

我有一张桌子

CREATE TABLE tb_incident (
  id int(10) unsigned NOT NULL AUTO_INCREMENT,
  what tinyint(4) NOT NULL,
  when bigint(20) unsigned NOT NULL,
  where tinytext NOT NULL,
  where_longitude_latitude point NOT NULL,
  who tinyint(4) DEFAULT NULL,
  why tinyint(4) DEFAULT NULL,
  description text,
  created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY id_UNIQUE (id),
  KEY index_main_items (what,when),
  SPATIAL KEY location_index (where_longitude_latitude)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

当我尝试运行此查询时,

SELECT db_incident.distance(point(132.6,15.604436765987833),where_longitude_latitude,'mi') AS "Distance in miles", 
X(where_longitude_latitude) AS "Longitude", 
Y(where_longitude_latitude) AS "Latitude" 
FROM db_incident.tb_incident 
WHERE db_incident.distance(point(132.6,15.604436765987833),where_longitude_latitude,'mi') <= 5
LIMIT 100

执行需要10分钟以上,有时会出现此错误错误代码:2013。查询期间与MySQL服务器的连接丢失

我的表有超过1000万条记录。有人可以看一下,看看是否有办法优化查询。

注意: db_incident.distance是一个返回两点之间距离的函数。

以下是查询的 EXPLAIN

+----+-------------+-------------+------+---------------+------+---------+------+----------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows     | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+----------+-------------+
|  1 | SIMPLE      | tb_incident | ALL  | NULL          | NULL | NULL    | NULL | 11689629 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+----------+-------------+
1 row in set (0.00 sec)

0 个答案:

没有答案