【问题标题】:Why doesn't mysql utilise this spatial index?为什么mysql不利用这个空间索引?
【发布时间】: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时是否使用索引。

标签: mysql key spatial


【解决方案1】:

根据MySQL documentation。 您必须在 WHERE 子句中使用 MBRContains() 或 MBRWithin() 等函数。

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2011-07-23
    相关资源
    最近更新 更多