bonobos/spree — pull request #119 by jordan-brough (2014-10-22 03:31:39+00:00)
Title: A couple perf fixes for /admin homepage

(NOTE: There are probably better long-term fixes for this but I'm just a bit worried about the load on the servers tomorrow morning when all the ninjas get online and start using the admin tool. We can wait until a sane time to actually deploy this, if we want to deploy it, of course.)

A couple perf fixes for /admin homepage

Total diff in development mode:

Before:

Completed 200 OK in 47340ms (Views: 43722.9ms | ActiveRecord: 3370.1ms)
47.62s (browser)
5.4 MB page size

After:

Completed 200 OK in 1769ms (Views: 1374.2ms | ActiveRecord: 337.8ms)
1.87s (browser)
37 KB page size

Details:

I noticed that /admin was quite slow (~9 seconds) on prod and *really* slow in development (~45 seconds!).

I think two things have happened:

1. We have a lot more orders now
2. We have a *lot* more promos as of today (we went from under 3,000 to over 50,000 total today)

Regarding item 1 - for whatever reason postgres really doesn't like "distinct" modifier when querying orders like this.

Before the changes to orders_controller:

Spree::Order Load (2190.6ms) SELECT DISTINCT "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."deleted_at" IS NULL AND ('t'='t') AND ("spree_orders"."completed_at" IS NOT NULL) ORDER BY "spree_orders"."completed_at" DESC LIMIT 15 OFFSET 0

After:

Spree::Order Load ( 1.4ms) SELECT "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."deleted_at" IS NULL AND ('t'='t') AND ("spree_orders"."completed_at" IS NOT NULL) ORDER BY "spree_orders"."completed_at" DESC LIMIT 15 OFFSET 0

Regarding item 2 - the current code for the Orders#index page queries *all* promotions (i.e. `SELECT "spree_promotions".* FROM "spree_promotions"`) and loads all 50,000 into ActiveRecord objects and then renders them into 50,000 `
` tags in the html page.

HMTL page size before update to orders/index.html: 5.4 MB (_mega_)
HTML page size after: 37.1 KB (_kilo_)