mysql 空间数据速度
我有一个表,其中包含 SMALLINT 类型的 x
和 y
字段以及 POINT 类型的 pt
字段,设置为 POINT(x
,y
);x
和 y
具有正常的不等式,pt
具有空间索引集。
分析典型查询
select sql_no_cache
count(0) from `table_name`
where (x between -50 and 50)
and (y between -50 and 50);
-- vs
set @g = GeomFromText('Polygon((-50 -50, 50 -50, 50 50, -50 50, -50 -50))');
select sql_no_cache
count(0) from `table_name`
where MBRContains(@g, `pt`);
...显示通过 的查询>x
和 y
速度快 1.5 倍:
10 次查询时为 3.45±0.10ms 与 4.61±0.14ms。x
和 y
始终为 INT,并且仅查询矩形(甚至正方形)区域。是的,这是刻在石头上的;-)
主要问题是:
我是否错过了一些关于不恰当的事情,或者在这种情况下空间数据是否过度杀伤力?
- MySQL版本是5.1.37
- DB引擎类型是MyISAM(默认)
- 当前表大小是5k行,10-30k计划投入生产。
我有一些使用 MySQL 的经验,但从未使用过空间数据类型和空间不确定性。
I have a table with (among others) x
and y
fields of SMALLINT type and pt
of POINT type, set to POINT(x
,y
);x
and y
have normal indecies and pt
has a spatial index set.
Profiling typical query
select sql_no_cache
count(0) from `table_name`
where (x between -50 and 50)
and (y between -50 and 50);
-- vs
set @g = GeomFromText('Polygon((-50 -50, 50 -50, 50 50, -50 50, -50 -50))');
select sql_no_cache
count(0) from `table_name`
where MBRContains(@g, `pt`);
... shows that query via x
and y
is 1.5 times faster:
3.45±0.10ms vs 4.61±0.14ms over 10 queries.x
and y
would always be INT and only rectangular (even square) areas would be queried. Yes, this is carved in stone ;-)
The main question is:
Have I missed something about indecies or is spatial data an overkill in such case?
- MySQL version is 5.1.37
- DB Engine type is MyISAM (default)
- Current table size is 5k rows, 10-30k planned in production.
I have had some experience with MySQL, but never worked with spatial data types and spatial indecies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有组合的 x & 吗? y 表上有索引吗?如果是这样,那么是的,这非常快。
我相信空间索引有更广泛的用途。多边形结构可以有许多顶点,而矩形是更通用构造的单一情况。
如果矩形边界区域足以满足您的需求,那么我宁愿建议您使用 x 和 y 场解决方案,而不是增加地理空间扩展功能的复杂性。
Do you have a combined x & y INDEX on the table? If so then yes, this is extremely fast.
I believe Spatial indexes have more broad use. A polygon structure can have many vertices and the rectangle is a single case of a more generic construct.
If a rectangular boundary area is enough for your needs then I would rather suggest you go with the x and y fields solution than adding the complexity of the geospatial extension features.