0

我编写了一个简单的 Rails API,它返回两个日期之间的所有对象。在 Rails 控制台中,数据库只查询一次,但 API 导致对同一请求进行多次查询。

这是控制器方法:

def historic_returns
  respond_to do |format|
    format.json do
      start_date = Date.new(params[:start_year].to_i, params[:start_month].to_i)
      end_date = Date.new(params[:end_year].to_i, params[:end_month].to_i)
      @result = ShillerDataMonth.records_between_two_dates(start_date, end_date)
      render :json => @result
    end
  end
end

这是控制器使用的模型中的类方法:

def self.records_between_two_dates(start_date, end_date)
  ShillerDataMonth.where("record_date >= ? AND record_date <= ?", start_date, end_date).order("record_date asc")
end

当我在 Rails 控制台中运行 ShillerDataMonth#records_between_two_dates 方法时,正如预期的那样,只有一个数据库查询:

>> ShillerDataMonth.records_between_two_dates(Date.new(2010, 01), Date.new(2012, 01))
  ShillerDataMonth Load (2.2ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE (record_date >= '2010-01-01' AND record_date <= '2012-01-01') ORDER BY record_date asc

但是,当我在 localhost /historic_returns.json?start_year=2010&start_month=1&end_year=2012&end_month=1 上访问此路径时,会出现多个查询:

Started GET "/historic_returns.json?start_year=2010&start_month=1&end_year=2012&end_month=1" for 127.0.0.1 at 2013-07-08 20:44:34 -0400
Processing by ShillerDataMonthsController#historic_returns as JSON
  Parameters: {"start_year"=>"2010", "start_month"=>"1", "end_year"=>"2012", "end_month"=>"1"}
  ShillerDataMonth Load (2.1ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE (record_date >= '2010-01-01' AND record_date <= '2012-01-01') ORDER BY record_date asc
  ShillerDataMonth Load (0.6ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
  ShillerDataMonth Load (0.6ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
  ShillerDataMonth Load (0.8ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1
  CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1

......每个月都这样......

为什么 API 有多个请求?我该如何解决?谢谢您的帮助!

4

2 回答 2

0

看起来您有 N+1 问题,您可以在此处阅读更多相关信息 - http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations

于 2013-07-09T02:40:31.613 回答
0

我在 Rails 控制台中做了一些试验,看起来 to_json 就是为什么要进行这么多不必要的数据库查询的原因。在 Rails 控制台中, to_json 命令是生成所有不必要查询的地方:

@result = ShillerDataMonth.records_between_two_dates(Date.new(2012, 1), Date.new(2013, 1))
  ShillerDataMonth Load (6.5ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE (record_date >= '2012-01-01' AND record_date <= '2013-01-01') ORDER BY record_date asc
>> @result.to_json #multiple queries also happen with the as_json method
  ShillerDataMonth Load (3.3ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2011.12' LIMIT 1
  ShillerDataMonth Load (0.6ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2011.12' LIMIT 1
  ShillerDataMonth Load (0.6ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2011.12' LIMIT 1
  ShillerDataMonth Load (0.5ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2011.12' LIMIT 1
  ShillerDataMonth Load (0.5ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2012.01' LIMIT 1
....

我通过向模型添加一个方法来解决这个问题:

def self.json_formatting(collection)
  result = []
  collection.each do |shiller_data_month|
    result << shiller_data_month.attributes
  end
  result
end

我在控制器中使用这种方法

def historic_returns
  respond_to do |format|
    format.json do
      start_date = Date.new(params[:start_year].to_i, params[:start_month].to_i)
      end_date = Date.new(params[:end_year].to_i, params[:end_month].to_i)
      @result = ShillerDataMonth.records_between_two_dates(start_date, end_date)
      render :json => ShillerDataMonth.json_formatting(@result)
    end
  end
end

总之,我通过编写自己的方法来解决多个查询的问题,该方法将对象集合转换为哈希集合,而不是使用 Rails 默认值(不确定 Rails 默认值是 to_json 还是 as_json)。

于 2013-07-09T21:25:59.823 回答