首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不出错的情况下使用MySQL 8的空间索引?

如何在不出错的情况下使用MySQL 8的空间索引?
EN

Stack Overflow用户
提问于 2020-04-01 15:27:31
回答 1查看 1.6K关注 0票数 0

我知道这个错误:

代码语言:javascript
运行
复制
3674 The spatial index on column 'position' will not be used by the
query optimizer since the column does not have an SRID attribute.
Consider adding an SRID attribute to the column.

因此,在阅读了本文:https://mysqlserverteam.com/geographic-indexes-in-innodb/之后,我决定将SRID添加到专栏中,我也试着阅读文档,但我仍然不明白这是什么。所以我照课文说的做,然后添加如下:

代码语言:javascript
运行
复制
DROP DATABASE IF EXISTS `gis`;

CREATE DATABASE IF NOT EXISTS `gis`
    DEFAULT CHARACTER SET utf8
    DEFAULT COLLATE utf8_general_ci;

USE `gis`;

DROP TABLE IF EXISTS user;
CREATE TABLE IF NOT EXISTS user (
    id           INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    firstname    VARCHAR(48) NOT NULL,
    gender       ENUM('male', 'female') NOT NULL,
    age          TINYINT UNSIGNED NOT NULL,
    position     POINT NOT NULL SRID 4326
);

ALTER TABLE user ADD SPATIAL INDEX(position);

然后我尝试添加一些行:

代码语言:javascript
运行
复制
INSERT INTO user (firstname, gender, age, position) VALUES ('Alexander', 'male', 34, POINT(63.429909, 10.393035));
INSERT INTO user (firstname, gender, age, position) VALUES ('Dina', 'female', 21, POINT(63.426300, 10.392481));
INSERT INTO user (firstname, gender, age, position) VALUES ('Martin', 'male', 32, POINT(63.422304, 10.432027));
INSERT INTO user (firstname, gender, age, position) VALUES ('Tina', 'female', 19, POINT(63.430603, 10.373038));
INSERT INTO user (firstname, gender, age, position) VALUES ('Kristin', 'female', 20, POINT(63.434858, 10.411359));
INSERT INTO user (firstname, gender, age, position) VALUES ('Mette', 'female', 33, POINT(63.420422, 10.403811));
INSERT INTO user (firstname, gender, age, position) VALUES ('Andres', 'male', 34, POINT(63.419488, 10.395722));
INSERT INTO user (firstname, gender, age, position) VALUES ('Sandra', 'female', 25, POINT(63.432053, 10.408738));
INSERT INTO user (firstname, gender, age, position) VALUES ('Kine', 'female', 29, POINT(63.432302, 10.412643));
INSERT INTO user (firstname, gender, age, position) VALUES ('Henrik', 'male', 25, POINT(63.421055, 10.443288));

但我错了:

代码语言:javascript
运行
复制
Error Code: 3643. The SRID of the geometry does not match the SRID of the column 'position'.
The SRID of the geometry is 0, but the SRID of the column is 4326.
Consider changing the SRID of the geometry or the SRID property of the column.

如果我删除SRID,那么当我使用EXPLAIN执行这样的查询时,索引就不能工作了:

代码语言:javascript
运行
复制
SET @distance = 3.5;
SET @my_place_lng = 63.431592;
SET @my_place_lat = 10.396210;

EXPLAIN SELECT
    id,
    firstname,
    gender,
    age,
    ST_Distance_Sphere(Point(@my_place_lng, @my_place_lat), position) AS distance_from_me
FROM user
WHERE
    ST_Contains(ST_MakeEnvelope(
        Point((@my_place_lng+(@distance/111)), (@my_place_lat+(@distance/111))),
        Point((@my_place_lng-(@distance/111)), (@my_place_lat-(@distance/111)))
    ), position)
ORDER BY distance_from_me ASC;

我能做什么?我甚至不知道在哪里读更多,没有看到任何我理解的例子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-01 16:00:31

使用ST_SRID函数指定几何图形的SRID值,即

包装点定义

代码语言:javascript
运行
复制
... , 34,          POINT(63.429909, 10.393035        ));

就像这样:

代码语言:javascript
运行
复制
... , 34, ST_SRID( POINT(63.429909, 10.393035) ,4326) );
          ^^^^^^^^                             ^^^^^^

注意,几何图形的SRID值也需要在示例查询中设置。

MySQL参考手册指出,在SRID0中创建几何学,然后将其转换为SRID4326,更好的选择是直接在SRID4326中创建几何学。

代码语言:javascript
运行
复制
... , 34, ST_PointFromText('POINT(63.429909, 10.393035)',4326) ));
          ^^^^^^^^^^^^^^^^^^                           ^^^^^^^

如果我们不想这样做,那么另一个(不太理想的)选择是将列的SRID值设置为默认值0,而不是4326。当我们不设置SRID值时,MySQL使用默认值0。但这样我们就可以在无单位平面坐标系下工作了。如果我们希望将点几何处理为GPS、地球度、纬度/经度坐标,那就不好了。

参考资料:

https://dev.mysql.com/doc/refman/8.0/en/gis-general-property-functions.html#function_st-srid

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60974497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档