1
我有一张包含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)
你可以在产品型号中显示代码吗?你有没有读过你的产品表的初始化程序? – SteveTurczyn
我没有想到这一点,但它是有道理的。我进行了弹性搜索的整合,导致问题并在模型实例化时强制导入。我删除它,一切按预期工作。感谢您指出了这一点。 @SteveTurczyn –
您是否使用默认范围? –