[英]Geokit-rails + MySQL: How to speed up search through latitude and longitude?
I have a database table that contains columns storing latitude
and longitude
coords (both have the decimal
data type). 我有一个数据库表,其中包含存储
latitude
和longitude
坐标的列(都具有decimal
数据类型)。 The table has about 500k rows through the searches are made. 该表通过搜索进行了约50万行。
The problem is that if I search rows that are in radius of 100-200 miles, the searches lasts about 140 seconds, which is not possible to put on production. 问题是,如果我搜索半径为100-200英里的行,搜索将持续约140秒,这不可能投入生产。
The stack: 堆栈:
I'd like to ask you for help on how to speed up the searches through geographical data. 我想请教您有关如何通过地理数据加快搜索速度的帮助。 Should I implement a search similar to kayak.com , where are displayed some results to users and the search is still running on the background?
我是否应该实施类似于kayak.com的搜索,在哪里向用户显示一些结果并且搜索仍在后台运行?
Thank you in advance. 先感谢您。
EDIT: 'stores' scheme 编辑:“商店”计划
create_table "stores", force: true do |t|
t.string "store_name"
t.string "address"
t.string "city"
t.string "state"
t.string "county"
t.string "zip_code"
t.decimal "latitude", precision: 10, scale: 6
t.decimal "longitude", precision: 10, scale: 6
end
There are no indexes on the table. 表格上没有索引。 I've a similar table in the database that has
latitude
and longitude
columns too, I tried to add indexes on them, but the loading time of the searches stayed the same. 我在数据库中有一个类似的表,该表也具有
latitude
和longitude
列,我尝试在它们上添加索引,但是搜索的加载时间保持不变。
I would start with putting a combined index on the longitude and latitude columns like this: 我将从在这样的经度和纬度列上放置组合索引开始:
class AddIndexToStoresLongitudeLatitude < ActiveRecord::Migration
def up
add_index :stores, [:latitude, :longitude]
end
def down
remove_index :stores, [:latitude, :longitude]
end
end
Best ways to solve this is using a geospatial index. 解决此问题的最佳方法是使用地理空间索引。 However neither
geokit
nor geocoder
do support them for relational databases. 然而无论
geokit
也不geocoder
都支持他们的关系数据库。 The rgeo
gem does. rgeo
gem可以。 It's however is not well documented. 但是,它没有很好的记录。 If you are doing some serios search stuff it probably will make sense to delegate this problem to elastic_search.
如果您正在做一些Serios搜索工作,将这个问题委托给elastic_search可能是有意义的。
If @spickermann answer won't work, IMO you should migrate to Postgres. 如果@spickermann的答案不起作用,IMO,您应该迁移到Postgres。 PostGis is a very powerfull tool with great performance.
PostGis是功能非常强大的强大工具。 http://postgis.net/
http://postgis.net/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.