加速铁路查询

时间:2017-05-31 18:01:17

标签: ruby-on-rails ruby ruby-on-rails-5

我有一张包含10k产品的桌子。当我在服务器重启后查询数据库时,服务器正在查询所有10k产品,然后限制响应中的15条记录。这是正常的吗?它导致我的页面需要23秒才能加载第一页。在第二个请求,一切看起来更好,但我仍然不明白为什么第一个查询抓住所有这些记录。

我的查询:

@products = Product.limit(15)

第一反应:

Started GET "/admin/admin_products/" for 50.255.94.246 at 2017-05-31 17:43:49 +0000
Cannot render console from 50.255.94.246! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
ActiveRecord::SchemaMigration Load (0.6ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by AdminProductsController#index as HTML
/home/ubuntu/workspace/app/models/product.rb:96: warning: key :description is duplicated and overwritten on line 96
Product Load (5.7ms)  SELECT  "products".* FROM "products" ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (12.2ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 1000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (6.4ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 2000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (9.6ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 3000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (9.2ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 4000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (9.1ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 5000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (9.9ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 6000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (9.4ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 7000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (59.5ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 8000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (131.0ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 9000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
Product Load (2.1ms)  SELECT  "products".* FROM "products" WHERE ("products"."id" > 10000) ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1000]]
  Rendering admin_products/index.html.erb within layouts/application
  Rendered layouts/_admin_portal.html.erb (0.6ms)
  Product Load (0.8ms)  SELECT  "products".* FROM "products" LIMIT $1  [["LIMIT", 15]]
  Rendered collection of admin_products/_product.html.erb [15 times] (2.8ms)
  Rendered admin_products/_paginate.html.erb (0.6ms)
  Rendered admin_products/index.html.erb within layouts/application (15.9ms)
  Rendered layouts/_flash_messages.html.erb (0.8ms)
Completed 200 OK in 23486ms (Views: 7643.7ms | ActiveRecord: 269.2ms)

我第二次执行查询:

Started GET "/admin/admin_products/" for 50.255.94.246 at 2017-05-31 
17:59:00 +0000
Cannot render console from 50.255.94.246! Allowed networks: 127.0.0.1, ::1, 
127.0.0.0/127.255.255.255
Processing by AdminProductsController#index as HTML
Rendering admin_products/index.html.erb within layouts/application
Rendered layouts/_admin_portal.html.erb (0.5ms)
Product Load (0.9ms)  SELECT  "products".* FROM "products" LIMIT $1  [["LIMIT", 15]]
Rendered collection of admin_products/_product.html.erb [15 times] (2.7ms)
Rendered admin_products/_paginate.html.erb (0.6ms)
Rendered admin_products/index.html.erb within layouts/application (10.8ms)
Rendered layouts/_flash_messages.html.erb (0.8ms)
Completed 200 OK in 26ms (Views: 23.5ms | ActiveRecord: 0.9ms)

1 个答案:

答案 0 :(得分:0)

@SteveTurczyn指出我的问题最终变得相当简单。问题涉及调用我的产品模型中的弹性搜索的导入功能。删除代码行后,一切都按预期工作。这是减慢过程的代码。

Product.includes(normal_model: [:normal_brand]).import force: true

我现在有一个控制器方法,可以在需要时调用导入功能。