MySQL中给定半径内的查询点
我创建了以下 MySQL 表来存储纬度/经度坐标以及每个点的名称:
CREATE TABLE `points` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`location` point NOT NULL,
PRIMARY KEY (`id`),
SPATIAL KEY `location` (`location`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
我正在尝试查询:
- 给定点的 n 英里半径内的所有点;
- 每个返回点与给定点的距离
我发现的所有示例都指使用最小外接矩形(MBR)而不是半径。该表包含大约 100 万个点,因此这种需求需要尽可能高效。
I have created the following MySQL table to store latitude/longitude coordinates along with a name for each point:
CREATE TABLE `points` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`location` point NOT NULL,
PRIMARY KEY (`id`),
SPATIAL KEY `location` (`location`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
I am trying to query:
- all points within an n mile radius of a given point;
- the distance of each returned point from the given point
All of the examples I have found refer to using a minimum bounding rectangle (MBR) rather than a radius. The table contains approximately 1 million points, so this need needs to be as efficient as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 MySQL 5.7+
假设我们有下面的简单表,
使用以下简单数据,
您将获得另一个点的给定范围内的点(注意:我们必须在多边形内部搜索)下面是 st 函数的组合:
您应该看到类似这样的结果: 作为
距离参考,如果我们删除约束,测试点的结果将如下所示:
注 1:该字段被称为lnglat 因为如果您将点视为 (x, y),那么这是正确的顺序,并且也是大多数函数(如点)接受参数
注 2 的顺序:您实际上无法利用空间如果要使用圆圈,则使用索引;另请注意,点字段可以设置为接受 null,但如果它可以为 null,则空间索引无法对其进行索引(索引中的所有字段都必须为非 null)。
注释 3:st_buffer 被认为(根据文档)不适合此用例
注释 4:上面的函数(特别是 st_distance_sphere)被记录为快速但不一定超级准确;如果您的数据对此非常敏感,请为搜索添加一些回旋空间并对结果集进行一些微调
For MySQL 5.7+
Given we have the following simple table,
With the following simple data,
You would get the points within a given range of another point (note: we have to search inside a polygon) with the following combination of st functions:
You should see something like this as a result:
For reference on distance, if we remove the constraint the result for the test point looks like this:
Note 1: the field is called lnglat since that's the correct order if you think of points as (x, y) and is also the order most functions (like point) accept the parameter
Note 2: you can't actually take advantage of spatial indexes if you were to use circles; also note that the point field can be set to accept null but spatial indexes can't index it if it's nullable (all fields in the index are required to be non-null).
Note 3: st_buffer is considered (by the documentation) to be bad for this use case
Note 4: the functions above (in particular st_distance_sphere) are documented as fast but not necessarily super accurate; if your data is super sensitive to that add a bit of wiggle room to the search and do some fine tuning to the result set
半径不能有效地转位。您应该使用边界矩形来快速获取您可能正在寻找的点,然后过滤半径之外的点。
Radius is not efficiently indexable. You should use the bounding rectangle to quickly get the points you are probably looking for, and then filter points outside of the radius.
我对半径为圆内的一个点执行了此操作
I did that for one point inside the circle with radius