【发布时间】:2017-11-09 20:05:31
【问题描述】:
MySQL/InnoDB+MyISAM 5.7.20...
给定:
mysql> desc city;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | mediumint(9) | NO | PRI | NULL | auto_increment |
| name | char(30) | NO | | NULL | |
| geo | geometry | NO | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
mysql> show index from city;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| city | 0 | PRIMARY | 1 | id | A | 798000 | NULL | NULL | | BTREE | | |
| city | 1 | geo | 1 | geo | A | 882478 | 32 | NULL | | SPATIAL | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
然而……
mysql> explain select distinct name from city where st_distance_sphere(@p, geo) < 5000;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | city | NULL | ALL | NULL | NULL | NULL | NULL | 882478 | 100.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
所以很明显(从 type = ALL)没有使用空间索引。有人可以向我解释为什么不? InnoDB 和 MyISAM 存储引擎都是一样的。
【问题讨论】:
-
你是正确的philipxy。您引用的问题确实解释了问题的原因和解决方案。谢谢
-
我只是用谷歌搜索了“st_distance_sphere mySql 空间索引未使用 null”。我只能推断你没有——为什么不呢? (谷歌我的cmets关于谷歌搜索问题。)不幸的是,大部分SO问题不应该发布,只是谷歌搜索。 (我添加了“null”,因为您没有任何 PK——不寻常。)我希望那里有更多有用的空间查询命中/提示等着您。祝你好运。
-
请提供
SHOW CREATE TABLE;它比DESCRIBE更具描述性。 -
大部分行不会在
5000内吗?看你使用5时是否使用索引。