MySQL/InnoDB+MyISAM 5.7.20...
Given:
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 | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
Yet...
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 |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
So it's clear (from the type = ALL) that the spatial index isn't being used. Can somebody explain to me why not? It's the same with both InnoDB and MyISAM storage engines.
As per MySQL documentation . You have to use function such as MBRContains() or MBRWithin() in the WHERE clause.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.