请记住,我将在lat / long对上执行计算,什么数据类型最适合与MySQL数据库一起使用?
当前回答
虽然它并不是所有操作的最佳选择,但如果你正在制作地图瓷砖或使用只有一个投影的大量标记(点)(例如Mercator,像谷歌Maps和许多其他滑头地图框架),我发现我所谓的“巨大坐标系”真的非常非常方便。基本上,你将x和y像素坐标存储在一些放大的地方——我使用缩放级别23。这有几个好处:
You do the expensive lat/lng to mercator pixel transformation once instead of every time you handle the point Getting the tile coordinate from a record given a zoom level takes one right shift. Getting the pixel coordinate from a record takes one right shift and one bitwise AND. The shifts are so lightweight that it is practical to do them in SQL, which means you can do a DISTINCT to return only one record per pixel location, which will cut down on the number records returned by the backend, which means less processing on the front end.
我在最近的一篇博客文章中谈到了这些: http://blog.webfoot.com/2013/03/12/optimizing-map-tile-generation/
其他回答
存储Lat Long值的理想数据类型是十进制(9,6)
这是在大约10cm的精度,而只使用5字节的存储。
例如:CAST(123.456789 as decimal(9,6))
MySQL使用double为所有浮点数… 所以使用double类型。在大多数情况下,使用float会导致不可预测的四舍五入值
使用DECIMAL(8,6)表示纬度(90到-90度),使用DECIMAL(9,6)表示经度(180到-180度)。小数点后6位对于大多数应用程序都是可以的。两者都应该“有符号”以允许为负值。
PostGIS中的空间函数比MySQL中的空间函数更具功能性(即不局限于BBOX操作)。看看:链接文本
基本上,这取决于你需要的定位精度。使用DOUBLE可以获得3.5nm的精度。DECIMAL(8,6)/(9,6)减小到16cm。FLOAT是1.7米…
这个非常有趣的表格有一个更完整的列表:http://mysql.rjweb.org/doc.php/latlng:
Datatype Bytes Resolution
Deg*100 (SMALLINT) 4 1570 m 1.0 mi Cities
DECIMAL(4,2)/(5,2) 5 1570 m 1.0 mi Cities
SMALLINT scaled 4 682 m 0.4 mi Cities
Deg*10000 (MEDIUMINT) 6 16 m 52 ft Houses/Businesses
DECIMAL(6,4)/(7,4) 7 16 m 52 ft Houses/Businesses
MEDIUMINT scaled 6 2.7 m 8.8 ft
FLOAT 8 1.7 m 5.6 ft
DECIMAL(8,6)/(9,6) 9 16cm 1/2 ft Friends in a mall
Deg*10000000 (INT) 8 16mm 5/8 in Marbles
DOUBLE 16 3.5nm ... Fleas on a dog