Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a N+1 query problem in the orders controller #2894

Merged

Conversation

rymai
Copy link
Contributor

@rymai rymai commented Oct 5, 2018

The description of the problem and the fix can be found in spree/spree#9026.

Logs before:

Processing by Spree::Admin::OrdersController#index as HTML
  Spree::User Load (2.3ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 ORDER BY "spree_users"."id" ASC LIMIT $2  [["id", 96], ["LIMIT", 1]]
  Spree::Role Load (1.0ms)  SELECT "spree_roles".* FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = $1  [["user_id", 96]]
  Rendering /Users/remy/.gem/ruby/2.5.1/gems/solidus_backend-2.6.0/app/views/spree/admin/orders/index.html.erb within spree/layouts/admin
   (1.0ms)  SELECT COUNT(*) FROM "spree_stores"
   (1.4ms)  SELECT COUNT(*) FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NOT NULL
  Spree::Order Exists (0.5ms)  SELECT  1 AS one FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NOT NULL LIMIT $1 OFFSET $2  [["LIMIT", 1], ["OFFSET", 0]]
  Spree::Order Load (1.5ms)  SELECT  "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NOT NULL ORDER BY "spree_orders"."completed_at" DESC LIMIT $1 OFFSET $2  [["LIMIT", 15], ["OFFSET", 0]]
  Spree::User Load (0.7ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 93], ["LIMIT", 1]]
  Spree::User Load (0.7ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 74], ["LIMIT", 1]]
  Spree::User Load (0.7ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 92], ["LIMIT", 1]]
  Spree::User Load (0.7ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 90], ["LIMIT", 1]]
  CACHE Spree::User Load (0.0ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 90], ["LIMIT", 1]]
  Spree::User Load (0.8ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 89], ["LIMIT", 1]]
  Spree::User Load (0.8ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 88], ["LIMIT", 1]]
  Spree::User Load (0.7ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 87], ["LIMIT", 1]]
  Spree::User Load (0.8ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 LIMIT $2  [["id", 85], ["LIMIT", 1]]

Logs after:

Started GET "/admin/orders" for ::1 at 2018-10-05 13:55:31 +0200
Processing by Spree::Admin::OrdersController#index as HTML
  Spree::User Load (0.7ms)  SELECT  "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = $1 ORDER BY "spree_users"."id" ASC LIMIT $2  [["id", 96], ["LIMIT", 1]]
  Spree::Role Load (0.5ms)  SELECT "spree_roles".* FROM "spree_roles" INNER JOIN "spree_roles_users" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles_users"."user_id" = $1  [["user_id", 96]]
  Rendering /Users/remy/.gem/ruby/2.5.1/gems/solidus_backend-2.6.0/app/views/spree/admin/orders/index.html.erb within spree/layouts/admin
   (0.5ms)  SELECT COUNT(*) FROM "spree_stores"
   (0.6ms)  SELECT COUNT(*) FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NOT NULL
  Spree::Order Exists (0.5ms)  SELECT  1 AS one FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NOT NULL LIMIT $1 OFFSET $2  [["LIMIT", 1], ["OFFSET", 0]]
  Spree::Order Load (0.9ms)  SELECT  "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NOT NULL ORDER BY "spree_orders"."completed_at" DESC LIMIT $1 OFFSET $2  [["LIMIT", 15], ["OFFSET", 0]]
  Spree::User Load (0.7ms)  SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" IN ($1, $2, $3, $4, $5, $6, $7, $8)  [["id", 93], ["id", 74], ["id", 92], ["id", 90], ["id", 89], ["id", 88], ["id", 87], ["id", 85]]

Fixes spree/spree#9026.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This is great.

Please rebase to get rid of the Netlify errors.

Fixes spree/spree#9026.

Signed-off-by: Rémy Coutable <remy@rymai.me>
@rymai rymai force-pushed the 9026-fix-n-plus-1-in-orders-controller branch from 28e6517 to 1245a3a Compare October 5, 2018 22:38
@rymai
Copy link
Contributor Author

rymai commented Oct 5, 2018

Thanks. This is great.

🎉

Please rebase to get rid of the Netlify errors.

Done, thanks!

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@rymai
Copy link
Contributor Author

rymai commented Oct 10, 2018

@kennyadsl Thanks! 🎉

@rymai rymai deleted the 9026-fix-n-plus-1-in-orders-controller branch October 10, 2018 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Orders controller has a N+1 query problem on the users table
3 participants