简体   繁体   English

如何加速 Rails 控制器查询?

[英]How to speed up a rails controller queries?

My app seams to be getting bogged down.我的应用程序接缝陷入困境。 Can someone can help me optimize this controller code to run faster?有人可以帮我优化这个控制器代码以运行得更快吗? Or point me in the right direction.或者指出我正确的方向。 I'm trying to display a list of customers which are defined by active is true and a list of potential customers which active is false.我正在尝试显示由 active 定义为 true 的客户列表和 active 为 false 的潜在客户列表。 Archived customers are archived true.存档的客户存档是真的。 Thank you.谢谢你。

if current_user.manager?
  get_customers = Customer.where(:archived => false)
  @cc = get_customers.where(:active => true)
  @current_customers = @cc.where(:user_id => current_user.id)
  @count_current = @current_customers.count

  @pc = get_customers.where(:active => false)
  @potential_customers = @pc.where(:user_id => current_user.id)
  @count_potential = @potential_customers.count
end

How does this look for improving speed?这如何提高速度?

model模型

scope :not_archived, -> { where(:archived => false) }
  scope :current_customers, -> { where(:active => true).not_archived }
  scope :potential_customers, -> { where(:active => false).not_archived }
  scope :archived_customers, -> { where(:archived => true) }

Controller控制器

 @current_customers = Customer.current_customers.includes(:contacts,:contracts)

View看法

link_to "Current Clients #{@count_current.size}"

You may find help here你可以在这里找到帮助

As @Gabbar pointed out and I will add to it, your app right now is eager-loading (opposite of lazy-loading ) which means that you are loading more from the database than needed.正如@Gabbar 指出的那样,我将添加到它,您的应用程序现在是急切加载(与延迟加载相反),这意味着您从数据库加载的内容超过了需要的内容。 What we need to do is optimize but that totally depends on your use-case.我们需要做的是优化,但这完全取决于您的用例。

Whatever the use-case, you can do a few common things to make things better: You can implement pagination (there are gems for it and you can do it yourself too) or infinite scrolling.无论用例是什么,您都可以做一些常见的事情来使事情变得更好:您可以实现分页(有一些宝石,您也可以自己完成)或无限滚动。 In this case, you will be loading a set amount of records from db at first but as soon as user wants more, either they will scroll down or click 'next' button and your action will be called again but with an increment in the page number which means get the next set of records.在这种情况下,您将首先从 db 加载一定数量的记录,但一旦用户想要更多记录,他们将向下滚动或单击“下一步”按钮,您的操作将再次被调用,但页面中会增加number 表示获取下一组记录。

Implementing based on scroll involves JS and the view-height etc. but pagination is much simpler.基于scroll的实现涉及JS和view-height等,但是分页就简单多了。

Gems:宝石:

kaminari gem雷宝石

infinite-pages无限页

Using includes使用includes

One more thing you must do is, use include in query if your records are related.您必须做的另一件事是,如果您的记录相关,请在查询中使用包含。 Using include is tricky but very very helpful in time-saving.使用 include 很棘手,但在节省时间方面非常有帮助。 It will fetch the related needed record together in one go from database unlike your code going to and fro database multiple times.与您的代码多次往返数据库不同,它将一次性从数据库中获取相关的所需记录。 Fetching from database takes a lot of time as compared to fetching from RAM.与从 RAM 中获取相比,从数据库中获取需要花费大量时间。

@users = User.all.includes(:comments) #comments for all users brought along with users but saved in RAM for future access.
@comments = @users.map(&:comments) # no need to go to db again, just RAM.

Using scopes in models:在模型中使用scopes

Creating scopes in models helps too.在模型中创建范围也有帮助。 In your case, you should create scopes like this:在您的情况下,您应该创建这样的范围:

  scope :archived_customers, -> { where('archived IS false') }
  scope :potential_customers, -> { where('active IS false') }

  **OR**

  scope :archived_customers, -> { where(:archived => false) }
  scope :potential_customers, -> { where(:active => false) }

Loading all the available records in a single query can be very costly.在单个查询中加载所有可用记录的成本可能非常高。 Moreover, a user may be interested only in a couple of the most recent records (ie, the latest posts in a blog) and does not want to wait for all records to load and render.此外,用户可能只对几个最近的记录(即博客中的最​​新帖子)感兴趣,并且不想等待所有记录加载和呈现。

There are couples of ways to sort out this problem有几种方法可以解决这个问题

example#1 implementation of Load More示例#1 Load More 的实现

example#2 implementation of Infinite Scrolling 无限滚动的示例#2 实现

example#3 implementation of pagination示例#3 分页的实现

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

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