Ruby on rails 从SQLite3升级到PG-db的速度要慢100倍

Ruby on rails 从SQLite3升级到PG-db的速度要慢100倍,ruby-on-rails,postgresql,sqlite,database-performance,Ruby On Rails,Postgresql,Sqlite,Database Performance,只是简化了我的开发环境,并从SQLite3切换到PG,所以在我的生产环境中也是如此。 我以前的工作代码现在比以前花费的时间多得多。 除了安装pg,没有任何改变 在PG之前: Processing by CollectionsController#show as HTML Parameters: {"keyword"=>"cat and dog towel", "id"=>"37"} Collection Load (0.2ms) SELECT "collections".

只是简化了我的开发环境,并从SQLite3切换到PG,所以在我的生产环境中也是如此。 我以前的工作代码现在比以前花费的时间多得多。 除了安装pg,没有任何改变

在PG之前:

Processing by CollectionsController#show as HTML
  Parameters: {"keyword"=>"cat and dog towel", "id"=>"37"}
  Collection Load (0.2ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = ? LIMIT ?  [["id", 37], ["LIMIT", 1]]
  Seller Load (0.2ms)  SELECT  "sellers".* FROM "sellers" WHERE "sellers"."id" = ? LIMIT ?  [["id", 13], ["LIMIT", 1]]
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
  Search Load (0.2ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = ? LIMIT ?  [["term", "cat and dog towel"], ["LIMIT", 1]]
  Listing Load (1.4ms)  SELECT  DISTINCT "listings".* FROM "listings" INNER JOIN "listings_searches" ON "listings"."id" = "listings_searches"."listing_id" WHERE "listings_searches"."search_id" = ? ORDER BY "listings"."id" ASC LIMIT ?  [["search_id", 775], ["LIMIT", 1]]
  Search Load (0.3ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = ? ORDER BY "searches"."id" ASC LIMIT ?  [["term", "Beach Towel"], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  Search Exists (0.2ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = ? LIMIT ?  [["term", "Beach Towel"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES (?, ?, ?)  [["term", "Beach Towel"], ["created_at", "2018-06-18 12:47:58.519223"], ["updated_at", "2018-06-18 12:47:58.519223"]]
   (7.0ms)  commit transaction
  Item Exists (0.3ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = ? AND "items"."collection_id" = 37 LIMIT ?  [["search_id", 776], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  Collection Load (0.1ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = ? LIMIT ?  [["id", 37], ["LIMIT", 1]]
  Item Exists (0.2ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = ? AND "items"."search_id" = 776 LIMIT ?  [["collection_id", 37], ["LIMIT", 1]]
  SQL (2.3ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["collection_id", 37], ["search_id", 776], ["created_at", "2018-06-18 12:47:58.540021"], ["updated_at", "2018-06-18 12:47:58.540021"]]
   (6.2ms)  commit transaction
Started GET "/sellers/16" for 127.0.0.1 at 2018-06-18 15:48:00 +0300
Processing by SellersController#show as HTML
  Parameters: {"id"=>"16"}
  Search Load (5.5ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = ? ORDER BY "searches"."id" ASC LIMIT ?  [["term", "Bath Towel"], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  Search Exists (0.2ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = ? LIMIT ?  [["term", "Bath Towel"], ["LIMIT", 1]]
  SQL (1.4ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES (?, ?, ?)  [["term", "Bath Towel"], ["created_at", "2018-06-18 12:48:01.250699"], ["updated_at", "2018-06-18 12:48:01.250699"]]
  Seller Load (0.3ms)  SELECT  "sellers".* FROM "sellers" WHERE "sellers"."id" = ? LIMIT ?  [["id", 16], ["LIMIT", 1]]
   (9.6ms)  commit transaction
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.3ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = ? AND "items"."collection_id" = 37 LIMIT ?  [["search_id", 777], ["LIMIT", 1]]
   (0.1ms)  begin transaction
  Collection Load (0.3ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = ? LIMIT ?  [["id", 37], ["LIMIT", 1]]
  Item Exists (0.2ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = ? AND "items"."search_id" = 777 LIMIT ?  [["collection_id", 37], ["LIMIT", 1]]
  SQL (1.0ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["collection_id", 37], ["search_id", 777], ["created_at", "2018-06-18 12:48:01.285942"], ["updated_at", "2018-06-18 12:48:01.285942"]]
  Shop Load (8.8ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."seller_id" = ? ORDER BY "shops"."id" DESC LIMIT ?  [["seller_id", 16], ["LIMIT", 1]]
   (40.2ms)  commit transaction
  Rendering sellers/show.html.erb within layouts/application
   (0.4ms)  SELECT COUNT(*) FROM "listings" WHERE "listings"."shop_id" = 12
  Listing Load (0.4ms)  SELECT  "listings".* FROM "listings" WHERE "listings"."shop_id" = 12 LIMIT ? OFFSET ?  [["LIMIT", 24], ["OFFSET", 0]]
  Rendered sellers/show.html.erb within layouts/application (4.8ms)
  Rendered layouts/_shim.html.erb (0.4ms)
  Rendered layouts/_rails_default.html.erb (93.9ms)
  Rendered layouts/_meta.html.erb (0.3ms)
  Seller Exists (0.3ms)  SELECT  1 AS one FROM "sellers" WHERE "sellers"."user_id" = ? LIMIT ?  [["user_id", 1], ["LIMIT", 1]]
  Seller Load (0.3ms)  SELECT  "sellers".* FROM "sellers" WHERE "sellers"."user_id" = ? ORDER BY "sellers"."id" DESC LIMIT ?  [["user_id", 1], ["LIMIT", 1]]
  Rendered layouts/_header.html.erb (5.5ms)
  Seller Load (0.3ms)  SELECT "sellers".* FROM "sellers" WHERE "sellers"."user_id" = ?  [["user_id", 1]]
  Shop Load (0.3ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."seller_id" = ? ORDER BY "shops"."id" DESC LIMIT ?  [["seller_id", 13], ["LIMIT", 1]]
  CACHE Shop Load (0.0ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."seller_id" = ? ORDER BY "shops"."id" DESC LIMIT ?  [["seller_id", 16], ["LIMIT", 1]]
  Rendered layouts/_sidebar.html.erb (7.3ms)
  Rendered layouts/_end_sidebar.html.erb (0.4ms)
  Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 212ms (Views: 120.5ms | ActiveRecord: 12.3ms)
PG之后:

Started POST "/collections/1/item/choose.25" for 127.0.0.1 at 2018-06-18 16:56:12 +0300
Processing by ItemsController#choose as 
  Parameters: {"authenticity_token"=>"11+Rpf2qXEKhQ5vBJLWie2EBg1b2Dtrw8iIgoLwXyvake7+myDrVErcqTwNcPYAZ5xs+zFKXaJjwM7fTakXaoA==", "collection_id"=>"1"}
  Collection Load (0.4ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Seller Load (0.6ms)  SELECT  "sellers".* FROM "sellers" WHERE "sellers"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Load (0.3ms)  SELECT  "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2  [["id", 25], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  Collection Load (0.5ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Search Load (0.9ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."id" = $1 LIMIT $2  [["id", 31], ["LIMIT", 1]]
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND ("items"."id" != $2) AND "items"."search_id" = 31 LIMIT $3  [["collection_id", 1], ["id", 25], ["LIMIT", 1]]
  SQL (0.8ms)  UPDATE "items" SET "chosen" = $1, "updated_at" = $2 WHERE "items"."id" = $3  [["chosen", "t"], ["updated_at", "2018-06-18 13:56:12.465841"], ["id", 25]]
   (2.7ms)  COMMIT
   (0.4ms)  BEGIN
  SQL (0.6ms)  UPDATE "collections" SET "keyword" = $1, "updated_at" = $2 WHERE "collections"."id" = $3  [["keyword", "Bookish Candles"], ["updated_at", "2018-06-18 13:56:12.473173"], ["id", 1]]
   (1.5ms)  COMMIT
  Search Load (0.6ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "Bookish Candles"], ["LIMIT", 1]]
  Listing Load (4.1ms)  SELECT  DISTINCT "listings".* FROM "listings" INNER JOIN "listings_searches" ON "listings"."id" = "listings_searches"."listing_id" WHERE "listings_searches"."search_id" = $1 ORDER BY "listings"."id" ASC LIMIT $2  [["search_id", 31], ["LIMIT", 3]]
  Search Load (0.7ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "soy candles"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Search Exists (0.8ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "soy candles"], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "soy candles"], ["created_at", "2018-06-18 13:56:12.491262"], ["updated_at", "2018-06-18 13:56:12.491262"]]
   (2.3ms)  COMMIT
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 46], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Collection Load (1.3ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 46 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (1.0ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 46], ["created_at", "2018-06-18 13:56:12.504381"], ["updated_at", "2018-06-18 13:56:12.504381"]]
   (1.1ms)  COMMIT
  Search Load (0.8ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "book candle"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Search Exists (0.5ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "book candle"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "book candle"], ["created_at", "2018-06-18 13:56:14.812727"], ["updated_at", "2018-06-18 13:56:14.812727"]]
   (2.8ms)  COMMIT
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 47], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  Collection Load (1.0ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 47 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 47], ["created_at", "2018-06-18 13:56:14.825865"], ["updated_at", "2018-06-18 13:56:14.825865"]]
   (1.4ms)  COMMIT
  Search Load (1.0ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "literary gifts"], ["LIMIT", 1]]
   (0.4ms)  BEGIN
  Search Exists (0.7ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "literary gifts"], ["LIMIT", 1]]
  SQL (0.7ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "literary gifts"], ["created_at", "2018-06-18 13:56:17.776368"], ["updated_at", "2018-06-18 13:56:17.776368"]]
   (2.4ms)  COMMIT
  Item Exists (0.8ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 48], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Collection Load (1.6ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.8ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 48 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (1.1ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 48], ["created_at", "2018-06-18 13:56:17.790424"], ["updated_at", "2018-06-18 13:56:17.790424"]]
   (1.2ms)  COMMIT
  Search Load (0.7ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "book candles"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Search Exists (0.7ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "book candles"], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "book candles"], ["created_at", "2018-06-18 13:56:20.872076"], ["updated_at", "2018-06-18 13:56:20.872076"]]
   (1.4ms)  COMMIT
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 49], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  Collection Load (0.6ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 49 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.8ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 49], ["created_at", "2018-06-18 13:56:20.883450"], ["updated_at", "2018-06-18 13:56:20.883450"]]
   (2.3ms)  COMMIT
  Search Load (0.9ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "literary candles"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Search Exists (0.5ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "literary candles"], ["LIMIT", 1]]
  SQL (0.8ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "literary candles"], ["created_at", "2018-06-18 13:56:22.523896"], ["updated_at", "2018-06-18 13:56:22.523896"]]
   (1.4ms)  COMMIT
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 50], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Collection Load (0.7ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.9ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 50 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.9ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 50], ["created_at", "2018-06-18 13:56:22.536553"], ["updated_at", "2018-06-18 13:56:22.536553"]]
   (2.2ms)  COMMIT
  Search Load (0.9ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "soy candle"], ["LIMIT", 1]]
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 18], ["LIMIT", 1]]
  Search Load (0.6ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "8oz candles"], ["LIMIT", 1]]
   (0.3ms)  BEGIN
  Search Exists (0.6ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "8oz candles"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "8oz candles"], ["created_at", "2018-06-18 13:56:25.455733"], ["updated_at", "2018-06-18 13:56:25.455733"]]
   (2.4ms)  COMMIT
  Item Exists (0.8ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 51], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Collection Load (0.7ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 51 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (1.0ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 51], ["created_at", "2018-06-18 13:56:25.468089"], ["updated_at", "2018-06-18 13:56:25.468089"]]
   (1.2ms)  COMMIT
  Search Load (0.7ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "handmade soy candles"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Search Exists (0.5ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "handmade soy candles"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "handmade soy candles"], ["created_at", "2018-06-18 13:56:27.445609"], ["updated_at", "2018-06-18 13:56:27.445609"]]
   (1.3ms)  COMMIT
  Item Exists (1.1ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 52], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Collection Load (0.5ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 52 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 52], ["created_at", "2018-06-18 13:56:27.457050"], ["updated_at", "2018-06-18 13:56:27.457050"]]
   (2.5ms)  COMMIT
  Search Load (0.7ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "bookish candle"], ["LIMIT", 1]]
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 13], ["LIMIT", 1]]
  Search Load (0.5ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "bookish candles"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Search Exists (0.5ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "bookish candles"], ["LIMIT", 1]]
  SQL (1.0ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "bookish candles"], ["created_at", "2018-06-18 13:56:29.102682"], ["updated_at", "2018-06-18 13:56:29.102682"]]
   (1.6ms)  COMMIT
  Item Exists (0.8ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 53], ["LIMIT", 1]]
   (0.3ms)  BEGIN
  Collection Load (0.5ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.5ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 53 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 53], ["created_at", "2018-06-18 13:56:29.114736"], ["updated_at", "2018-06-18 13:56:29.114736"]]
   (2.1ms)  COMMIT
  Search Load (1.0ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "personalized gifts"], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Search Exists (0.8ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "personalized gifts"], ["LIMIT", 1]]
  SQL (0.8ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "personalized gifts"], ["created_at", "2018-06-18 13:56:30.446268"], ["updated_at", "2018-06-18 13:56:30.446268"]]
   (1.5ms)  COMMIT
  Item Exists (1.0ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 54], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Collection Load (0.9ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 54 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.8ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 54], ["created_at", "2018-06-18 13:56:30.459078"], ["updated_at", "2018-06-18 13:56:30.459078"]]
   (2.2ms)  COMMIT
  Search Load (0.7ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "personalized gift"], ["LIMIT", 1]]
   (0.3ms)  BEGIN
  Search Exists (0.5ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "personalized gift"], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "personalized gift"], ["created_at", "2018-06-18 13:56:32.460438"], ["updated_at", "2018-06-18 13:56:32.460438"]]
   (2.5ms)  COMMIT
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 55], ["LIMIT", 1]]
   (0.2ms)  BEGIN
  Collection Load (0.7ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.7ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 55 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.7ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 55], ["created_at", "2018-06-18 13:56:32.474614"], ["updated_at", "2018-06-18 13:56:32.474614"]]
   (1.1ms)  COMMIT
  Search Load (0.8ms)  SELECT  "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2  [["term", "book smell"], ["LIMIT", 1]]
   (0.5ms)  BEGIN
  Search Exists (0.8ms)  SELECT  1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2  [["term", "book smell"], ["LIMIT", 1]]
  SQL (0.7ms)  INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["term", "book smell"], ["created_at", "2018-06-18 13:56:34.573441"], ["updated_at", "2018-06-18 13:56:34.573441"]]
   (1.3ms)  COMMIT
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2  [["search_id", 56], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  Collection Load (1.4ms)  SELECT  "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Item Exists (0.6ms)  SELECT  1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 56 LIMIT $2  [["collection_id", 1], ["LIMIT", 1]]
  SQL (0.6ms)  INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["collection_id", 1], ["search_id", 56], ["created_at", "2018-06-18 13:56:34.585728"], ["updated_at", "2018-06-18 13:56:34.585728"]]
   (2.5ms)  COMMIT
Redirected to http://localhost:3000/collections/1
Completed 302 Found in 24422ms (ActiveRecord: 153.5ms)
我插入的项目数量各不相同,但即使在许多不同的示例中,PG始终需要更长的时间,即使在查看我发布的这两个日志时,SQLite3似乎每个事务都需要更长的时间。 这是正常的还是我可以做些什么来优化它


谢谢

看起来
ActiveRecord
对这两个数据库的响应都非常快:

Sqlite:
ActiveRecord:12.3ms

Postgres:
ActiveRecord:153.5ms

虽然Postgres肯定更高,但我认为这两种反应时间都是可以接受的,而且每次你做出反应时,反应时间都会略有不同

但是,您确实发生了某种重定向
302重定向到http://localhost:3000/collections/1
这似乎给请求增加了很多时间

您可以尝试确保该页面上没有任何脚本会破坏工作。此外,您可以确保索引已应用于Postgres数据库,还可以确保集合页面没有任何
n+1
查询


由于问题中给出的信息有限,很难说更多,

您能给我们一个提示,说明我们应该看什么吗?所有这些查询似乎都在1ms以下,所以我不确定我对精确的时间安排有多大信心。我想我说的是“你有问题吗”?