使用acts_as_votable gem时的N + 1查询

时间:2016-10-28 06:47:46

标签: ruby-on-rails

我使用acts_as_votable gem来管理投票。对于此GET请求,此处似乎发生了“N + 1查询问题”。我尝试添加includes(:votes)来解决它,但它没有用。

以下是我的查询。我使用了decent_exposure。

expose(:words) { Word.includes(:user, :categories).sorted.paginate(:page => params[:page]).all }

您是否知道如何解决此问题?

Started GET "/words?locale=pl" for 127.0.0.1 at 2016-10-28 08:37:41 +0200
Processing by WordsController#index as HTML
  Parameters: {"locale"=>"pl"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Word Load (2.0ms)  SELECT  "words".* FROM "words"  ORDER BY created_at DESC LIMIT 20 OFFSET 0
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (1)
  WordCategory Load (0.6ms)  SELECT "word_categories".* FROM "word_categories" WHERE "word_categories"."word_id" IN (2232, 2231, 2230, 2229, 2228, 2227, 2226, 2225, 2224, 2223, 2222, 2221, 2220, 2219, 2218, 2217, 2216, 2215, 2214, 2213)
  Category Load (0.6ms)  SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1)
   (0.8ms)  SELECT COUNT(*) FROM "words"
   (0.8ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2232], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.6ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2232], ["votable_type", "Word"], ["vote_flag", "f"]]
   (1.3ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", "t"]]
   (0.5ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2231], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.6ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2231], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.5ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2230], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.5ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2230], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2229], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2229], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2228], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2228], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2227], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2227], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2226], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2226], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2225], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2225], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2224], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2224], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2223], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2223], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2222], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2222], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2221], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2221], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2220], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2220], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.2ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2219], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2219], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2218], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2218], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2217], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2217], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2216], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2216], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2215], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2215], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2214], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.2ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2214], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2213], ["votable_type", "Word"], ["vote_flag", "t"]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 2213], ["votable_type", "Word"], ["vote_flag", "f"]]
  CACHE (0.0ms)  SELECT COUNT(*) FROM "words" WHERE "words"."user_id" = $1 AND "words"."verified" = $2  [["user_id", 1], ["verified", true]]
  Rendered words/_words.html.haml (268.3ms)
  Rendered words/index.html.haml within layouts/application (280.5ms)
  Rendered shared/_ga.html.erb (0.5ms)
  Rendered shared/_header.html.haml (15.3ms)
  Rendered shared/_alerts.html.haml (1.6ms)
  Rendered shared/_footer.html.haml (1.0ms)
Completed 200 OK in 1059ms (Views: 1028.9ms | ActiveRecord: 29.4ms)

0 个答案:

没有答案