简体   繁体   English

加快搜索大型 json 数组(用于 jQuery/Rails 自动完成文本框)

[英]Speeding up searching through large json array (for jQuery/Rails autocomplete text box)

I have a location text field hooked up to the jquery plugin EasyAutocomplete :我有一个连接到 jquery 插件EasyAutocomplete的位置文本字段:

在此处输入图像描述

It's working, but the problem is the dataset is large - 100,000 records or so.它正在工作,但问题是数据集很大 - 100,000 条左右的记录。 It takes an unacceptably long time to match any results.匹配任何结果都需要非常长的时间。

I have a json endpoint in my app that loads the records for the autocomplete.我的应用程序中有一个 json 端点,用于加载自动完成的记录。 The code in the Rails controller looks like this: Rails controller 中的代码如下所示:

def index
  @locations = Location.select(:id,:canonical_name)

  respond_to do |format|
    format.json { render json: @locations }
  end
end

So far what I've tried is putting an index on the canonical_name column, and putting a Rails.cache.fetch around the loading of the records, but neither of these things helped much.到目前为止,我尝试的是在canonical_name列上放置一个索引,并在加载记录时放置一个Rails.cache.fetch ,但这些都没有多大帮助。

What can be done to speed up this operation?可以做些什么来加快此操作?

The problem here is not searching through a large JSON array.这里的问题不是搜索大型 JSON 数组。 Its that you're not doing the search in the database in the first place.它首先不是在数据库中进行搜索。

You're first selecting 100,000 rows from your database and using it to initialize 100,000 model objects.您首先从数据库中选择 100,000 行并使用它来初始化 100,000 个 model 对象。 That not only will take a lot of time but eat through a gigantic chunk of RAM.这不仅会花费大量时间,而且会消耗大量 RAM。

You then go through and serialize that collection of 100,000 rows into a mega long JSON string which eats even more RAM.然后,您通过 go 并将 100,000 行的集合序列化为一个超长的 JSON 字符串,该字符串会占用更多的 RAM。

This is then sent across the internet to the poor client that gets a monster JSON response and runs out of data on their mobile plan.然后通过互联网将其发送给可怜的客户端,该客户端得到一个怪物 JSON 响应并用完他们的移动计划中的数据。 Even if you implemented some kind of magic stack sort that's phenomenally fast the costs if just getting the data here are staggering.即使您实现了某种神奇的堆栈排序,如果只是在此处获取数据,其成本也会非常惊人。

Caching might slightly speed up the server side but its going to do nothing for the initial cold cache hit or the amount of data sent across the wire and the amount of RAM this will use on the client.缓存可能会稍微加快服务器端的速度,但它不会对初始冷缓存命中或通过线路发送的数据量以及将在客户端使用的 RAM 量做任何事情。

Implementing a server side search like that provided by geonames or google is actually quite an undertaking and you might want to investigate the existing options.实现像 geonames 或 google 提供的服务器端搜索实际上是一项艰巨的任务,您可能想要调查现有的选项。

A really naive implementation would be:一个非常幼稚的实现是:

@locations = Location.where(
  "canonical_name like ?", "%#{params[:query]}%"
).select(:id, :canonical_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM