为什么mysql不利用这个空间索引?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么mysql不利用这个空间索引?相关的知识,希望对你有一定的参考价值。
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存储引擎都是一样的。
答案
根据MySQL documentation。您必须在WHERE子句中使用诸如MBRContains()或MBRWithin()之类的函数。
以上是关于为什么mysql不利用这个空间索引?的主要内容,如果未能解决你的问题,请参考以下文章