3

当我在我的应用程序中使用 rack-mini-profiler(运行 ruby​​ 1.9.3 p194、rails 3.2.8 和 pg 0.14.0)时,我得到了非常奇怪的分析输出。通常它看起来像这样:

/da/brugte-biler/Fiat/Multipla autouncle.dev on Mon, 27 Aug 2012 13:30:50 GMT
                                             duration (ms) from start (ms) query time (ms)
GET http://autouncle.dev:80/da/brugte-biler/F...   19.2 +0.0   3 sql   0.5
  Executing action: search                         83.4 +16.0  48 sql  9.4
   Rendering: cars_search/table_listing            819.2  +99.0  356 sql   71.2
    Rendering: cars/_reliability_report            12.5 +601.0
    Rendering: cars_search/_sidebar                1093.7 +616.0   281 sql   356.7
    Rendering: cars/_listing_table                 1612.6 +1731.0  123 sql   549.5
    Rendering: kaminari/_paginator                 27.2 +3632.0
   Rendering: layouts/search_results               31.3 +3671.0
    Rendering: layouts/_shared_head_content        223.4  +3673.0
    Rendering: partials/_top_links                 2.6  +3907.0
    Rendering: partials/_search_form               165.1  +3912.0
    Rendering: partials/_footer_links              5.2  +4086.0
    Rendering: layouts/_shared_footer_content      8.7  +4099.0
share show trivial show time with children   24.0 % in sql

这里奇怪的是,也就是说,有356条sql语句。但因为没有。如果我点击它们,看看叫什么,我会看到很多这样的陈述:

8.70 ms 
Executing action: search — 7.70 ms
Executing action: search
T+23.7 ms
Reader
0.3 ms
app/models/car_search.rb:97:in `relation'
app/controllers/cars_search_controller.rb:102:in `pagination_relation'
app/controllers/cars_search_controller.rb:37:in `search'
app/controllers/controller_concerns/country_requesting.rb:66:in `select_database_shard'
lib/www_middleware.rb:11:in `call'
SELECT 1   
Executing action: search
T+25.8 ms
Reader
0.2 ms
app/models/car_search.rb:97:in `relation'
app/controllers/cars_search_controller.rb:102:in `pagination_relation'
app/controllers/cars_search_controller.rb:37:in `search'
app/controllers/controller_concerns/country_requesting.rb:66:in `select_database_shard'
lib/www_middleware.rb:11:in `call'
SELECT 1   
Executing action: search
T+26.8 ms
Reader
0.2 ms
app/models/car_search.rb:97:in `relation'
app/controllers/cars_search_controller.rb:102:in `pagination_relation'
app/controllers/cars_search_controller.rb:37:in `search'
app/controllers/controller_concerns/country_requesting.rb:66:in `select_database_shard'
lib/www_middleware.rb:11:in `call'
SELECT 1

它就这样持续了 356 次。

我很困惑该怎么做。关于这可能是什么的任何线索?

更新:

car_search.rb 中的代码是一个非常繁重的方法,它构建了相关的查询。基本上它的作用与此类似:

def relation
  cars = Car.onsale # a scope that says .where(:sales_state => 'onsale')

  cars = cars.with_brand(brand) if brand.present? # a scope that says .where(:brand => 'some_brand')

  cars = cars.with_model_name(model_name) if model_name.present? # a scope that says .where(:model_name => 'some_model_name')

  return cars
end
4

0 回答 0