繁体   English   中英

Rails数据库查询ROR

[英]Rails Database Query ROR

首先,我对ROR还是陌生的,我正在尝试提出一种更有效的方法来对我的数据表进行数据库查询。

我的模特协会

class Store < ActiveRecord::Base
  has_many :surveys
  has_many :customers 
end
...
class Survey < ActiveRecord::Base
  belongs_to :store
end
...
class Customer < ActiveRecord::Base
  belongs_to :store
end

在我的数据表中

          <tbody>
            <% @store.surveys.each do |survey| %>
            <tr class="th-container table-fixer">
              <td><%= find_customer_id(survey.email).nil? ? survey.first_name : link_to(survey.first_name, store_customer_path(@store, find_customer_id(survey.email))) %></td>
              <td><%= get_ltv(survey) %></td>         
            </tr>
            <% end %>
          </tbody>

find_customer_id和get_ltv方法如下

def find_customer_id(customer_email)
   BwCustomer.find_by(email: customer_email)
end

代码存在的问题是,当前我遍历了1000个活动记录对象,当单击find_customer_id方法时,它将找到具有给定电子邮件地址的客户,并且查询要花15秒钟以上的时间来处理。

以我的情况,解决此问题的最佳方法是什么?

我有以下解决方案:1.将表连接到,这样我就不必调用另一个表了。2.延迟加载,仅在需要时加载对象

一些建议将不胜感激

谢谢

您通过电子邮件ID查询不会花费太多时间。

  1. 添加索引客户表中的电子邮件列(请参阅本作将通过活动记录迁移指标- http://apidock.com/rails/v4.2.1/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index

  2. 您的代码显示两次调用find_customer_id 这样做一次,仅触发1个数据库查询

  3. 您无需编写包装方法Customer.find_by_email(customer_email)也可以使用

为了进一步优化,您可以在一个循环中收集检查数据库中是否存在所需的所有客户ID,并触发一个数据库查询: Customer.where(email: [list of customer emails])

主要问题是您缺少客户和调查之间的关联。 您可以通过加入电子邮件来创建一个

class Survey < ActiveRecord::Base
  belongs_to :customer, primary_key: :email, foreign_key: :email
end

但这是一种粗略的方法。 您的应用程序在填写调查表时是否知道客户的ID? 还是可以由任何人填写这些调查,如果有人声称与客户拥有相同的电子邮件,则您可以链接吗?

无论如何,您都需要对两个电子邮件列都进行索引,并且如果在两者之间建立了关联,则可以在控制器代码中编写以下内容。

@store = Store.includes(surveys: :customer).find(params[store_id])

这将使数据库查询,它渴望加载您将要显示的所有调查和客户,以便在循环内可以使用survey.customer而不为每一行调用新查询。

暂无
暂无

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

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