首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加速rails查询

加速rails查询
EN

Stack Overflow用户
提问于 2017-05-31 18:01:16
回答 1查看 76关注 0票数 1

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

我的问题是:

代码语言:javascript
复制
@products = Product.limit(15)

第一次答复:

代码语言:javascript
复制
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)

第二次执行查询时:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-01 19:10:22

@SteveTurczyn指出,我的问题最后变得相当简单。这个问题涉及到在我的产品模型中调用一个用于弹性搜索的导入函数。在我删除了代码行之后,一切都如愿以偿。以下是减缓这一过程的代码。

代码语言:javascript
复制
Product.includes(normal_model: [:normal_brand]).import force: true

现在我有了一个控制器方法,在需要时调用导入函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44291755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档