gpt4 book ai didi

mysql - Rails 和 MySQL 上经纬度的最佳列类型

转载 作者:可可西里 更新时间:2023-11-01 06:33:20 26 4
gpt4 key购买 nike

我想知道在 MySQL + Rails 上存储纬度/经度的最佳列类型是什么。

  • 精度必须足以存储从移动设备和/或地理编码器获得的每一位纬度/经度。
  • 为了获得最佳查询性能,存储要求应该最低。

来自谷歌官方文档:

http://code.google.com/apis/maps/articles/phpsqlajax_v3.html

With the current zoom capabilities of Google Maps, you should only need 6 digits of precision after the decimal. To keep the storage space required for your table at a minimum, you can specify that the lat and lng attributes are floats of size (10,6). That will let the fields store 6 digits after the decimal, plus up to 4 digits before the decimal, e.g. -123.456789 degrees.

所以,实际上 FLOAT(10,6) 是 Google 推荐的。

但是,对于 Rails 3,似乎没有简单的方法来定义小数点后精确的 FLOAT 列类型。例如,您可以使用原始 SQL 编写迁移,如下所示:

def self.up
execute <<-SQL
ALTER TABLE places
ADD `lat` FLOAT(10,6),
ADD `lng` FLOAT(10,6)
SQL
add_index :places, [ :lat, :lng ]
end

但是 schema.rb 结果将如下所示:

t.float "lat", :limit => 10
t.float "lng", :limit => 10

缺少小数部分的精度。

在这里我可以看到几个选项:

  • 使用 FLOAT(10,6) 以获得最佳生产性能,并且不要在开发中转储架构(例如 rake db:test:load)。
  • 使用 Rails 支持的 DECIMAL(10,6),但占用 6 个字节,比 FLOAT 大 1.5 倍(参见:http://dev.mysql.com/doc/refman/5.1/en/storage-requirements.html)。也许这是一个很好的折衷方案?
  • 使用 DOUBLE,它比 Google 的要求宽敞得多,占用 8 个字节,比 FLOAT 大 2 倍。这也很简单。

你有什么建议?

最佳答案

KISS(即提交给框架),字节数不多。如果您使用特定的索引,它只会更重要。

关于mysql - Rails 和 MySQL 上经纬度的最佳列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6351729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com