2017-05-31 72 views
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) 
+1

你可以在產品型號中顯示代碼嗎?你有沒有讀過你的產品表的初始化程序? – SteveTurczyn

+3

我沒有想到這一點,但它是有道理的。我進行了彈性搜索的整合,導致問題並在模型實例化時強制導入。我刪除它,一切按預期工作。感謝您指出了這一點。 @SteveTurczyn –

+0

您是否使用默認範圍? –

回答

0

@SteveTurczyn指出我的問題,它結束了相當簡單。問題涉及爲我的產品模型中的彈性搜索調用導入函數。在我刪除了這行代碼之後,所有事情都按預期工作。這是減慢過程的代碼。

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

我現在有一個控制器方法,在需要時調用導入功能。