简体   繁体   English

Geokit-rails + MySQL:如何通过经纬度加快搜索速度?

[英]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). 我有一个数据库表,其中包含存储latitudelongitude坐标的列(都具有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: 堆栈:

  1. Rails 4 滑轨4
  2. MySQL 5.5 MySQL 5.5
  3. Geokit-rails gem Geokit-rails宝石

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. 我在数据库中有一个类似的表,该表也具有latitudelongitude列,我尝试在它们上添加索引,但是搜索的加载时间保持不变。

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.

相关问题 如何加快此MySQL查询的速度,以找到最接近给定纬度/经度的位置? - How can I speed up this MySQL query that finds the closest locations to a given latitude/longitude? 如何使用php和mysql使用经度和纬度进行几何搜索 - How to do a geometric search using latitude and longitude using php and mysql 如何使用Ruby on Rails将经度和纬度标记保存到mysql数据库中 - How to save a marker with latitude and longitude into mysql database using Ruby on Rails 来自自定义多边形MySQL的纬度经度搜索 - Latitude Longitude search from custom polygon MySQL Rails 和 MySQL 上的经纬度的最佳列类型 - Optimal column type for latitude and longitude on Rails and MySQL 如何加快mysql表的搜索速度? - How to speed up a search on the mysql table? 如何加快MySQL表中的搜索速度(无索引) - How to speed up search in MySQL table (No Index) 如何将纬度/经度数据正确导入MySQL? - How to import latitude/longitude data correctly into MySQL? Android向Mysql发送经纬度的方法 - How to send latitude and longitude from Android to Mysql 如何在MySQL中使用经度和纬度搜索最近的用户,同时按性别和年龄进行过滤? - How can I search for nearest users using longitude and latitude but also filter by sex and age in an efficient way in MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM