简体   繁体   English

几个使用大型 JQuery 自动完成数据集的用户正在减慢整个 Ruby on Rails 应用程序

[英]Several Users with Large JQuery Autocomplete Data Sets Slowing Down Entire Ruby on Rails App

Just wondering if I can get some suggestions from more senior programmers on perhaps a better way to load large quantities of data in our daycare app.只是想知道我是否可以从更高级的程序员那里获得一些建议,以了解在我们的日托应用程序中加载大量数据的更好方法。

The app is very basic.该应用程序非常基本。 When the user signs in, it loads all their kids so that they can schedule daycare appointments for them.当用户登录时,它会加载他们所有的孩子,以便他们可以为他们安排日托预约。

On the homepage, we have the appointment calendar.在主页上,我们有约会日历。 When you click a certain date, an appointment creation modal shows up.当您单击某个日期时,会显示约会创建模式。 This modal utilizes JQuery Autocomplete so that the business owner can start typing the kid/parent name and then select them from the list.此模式使用 JQuery 自动完成功能,以便企业主可以开始输入孩子/父母的姓名,然后从列表中输入 select。 Because of this need for JQuery Autocomplete, every kid is loaded into a JSON string when the homepage is loaded.由于需要 JQuery 自动完成功能,因此在加载主页时,每个孩子都会被加载到 JSON 字符串中。

kids_for_autocomplete = []
Kid.where(business_id: current_user.business_id).order_by_name.pluck(:id, :name, :parent_name, :archived).each do |kid_id, kid_name, parent_name, archived|
  kids_for_autocomplete << {:label => "#{kid_name} ( #{parent_name} )", :value => "#{kid_name} ( #{parent_name} )", :id => "#{kid_id}"} unless archived == true
end

@kids_for_autocomplete = kids_for_autocomplete.to_json

So, most users have between 100 and 300 kids in their database and the load time is only several hundred milliseconds.因此,大多数用户的数据库中有 100 到 300 个孩子,加载时间只有几百毫秒。 However, we have a couple users with over 3,000 kids in their database.但是,我们有几个用户,他们的数据库中有超过 3,000 个孩子。 When they visit the homepage, their loading time is usually over 5000 milliseconds.当他们访问主页时,他们的加载时间通常超过 5000 毫秒。 This slows down requests for everyone else on the same Heroku dyno.这会减慢同一 Heroku 测功机上其他所有人的请求。

A possible solution would be to load the Kids each time a calendar date is clicked and the modal appears.一个可能的解决方案是每次单击日历日期并出现模式时加载孩子。 This will increase the amount of requests since most users stay on the homepage and click the calendar a lot.这将增加请求的数量,因为大多数用户停留在主页上并经常点击日历。 For small users, loading the kids once for Autocomplete makes sense, since then the kids are loaded on the homepage for future requests (as long as they don't navigate off the homepage).对于小用户来说,为自动完成加载一次孩子是有意义的,从那时起,孩子会被加载到主页上以供将来的请求(只要他们不离开主页)。 I hope that makes sense.我希望这是有道理的。

We're just trying to figure out a way to speed up the requests for these users that have several thousands kids.我们只是想找出一种方法来加快这些拥有数千个孩子的用户的请求。

Any ideas are appreciated!任何想法表示赞赏!

Do you know exactly where your performance bottleneck is?你知道你的性能瓶颈到底在哪里吗? Without knowing exactly what part of this is slow, a couple of things that I would try before changing the overall design of the feature would be to use #find_each instead of #each on your Active Record relation.在不确切知道其中哪一部分很慢的情况下,在更改功能的整体设计之前我会尝试的几件事是在 Active Record 关系上使用#find_each而不是#each So,所以,

Kid.where(business_id: current_user.business_id).order_by_name.pluck(:id, :name, :parent_name, :archived).find_each do

That'll avoid loading thousands of records into memory all at once in order build up your JSON.这样可以避免一次将数千条记录加载到 memory 中,以便构建您的 JSON。

I'd also add a database index on kids.business_id if you don't have one already.如果您还没有,我还会在kids.business_id上添加一个数据库索引。

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

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