2017-05-31 2 views
1

Ich habe eine Tabelle, die 10k Produkte enthält. Wenn ich nach einem Serverneustart die Datenbank abfrage, fragt der Server alle 10k-Produkte ab und beschränkt dann auf 15 Datensätze in der Antwort. Ist das normal? Es verursacht, dass meine Seite 23 Sekunden für das Laden der ersten Seite benötigt. Nach der zweiten Anfrage sieht alles besser aus, aber ich verstehe immer noch nicht, warum die erste Abfrage alle diese Datensätze erfasst.Beschleunigung Schienen Anfragen

Meine Suche:

@products = Product.limit(15) 

Die erste Reaktion:

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) 

Das zweite Mal ausführen ich die Abfrage:

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

Können Sie den Code in Ihrem Produktmodell anzeigen? Und haben Sie Initialisierer, die Ihre Produkttabelle lesen? – SteveTurczyn

+3

Ich habe nicht daran gedacht, aber es macht Sinn. Ich hatte eine Integration für die elastische Suche, die das Problem verursachte und einen Import erzwang, wenn das Modell instanziiert wurde. Ich entfernte es und alles funktionierte wie erwartet. Danke, dass du darauf hingewiesen hast. @SteveTurczyn –

+0

Verwenden Sie einen Standardbereich? –

Antwort

0

@SteveTurczyn mein Problem hingewiesen, das endete als ziemlich einfach. Das Problem umfasste das Aufrufen einer Importfunktion für die elastische Suche, die in meinem Produktmodell enthalten war. Nachdem ich die Codezeile entfernt hatte, funktionierte alles wie erwartet. Hier ist der Code, der den Prozess verlangsamt hat.

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

Ich habe jetzt eine Controller-Methode, die die Importfunktion aufruft, wenn nötig.