I have an E-Commerce Rails Application where we need to output Orders placed by Customers on a page within last one year for reporting reasons. Now, the data set is quite large and displaying these Orders on a single page takes quite a bit of SQL processing. This task initially was very slow and hence I moved all the required order details to a Redis Server and fetching of data has become really fast now but we are still not quite there.
Here’s what we have:
Rendered **path**/sales_orders.html.haml within layouts/admin (39421.1ms)
Completed 200 OK in 44925ms (Views: 39406.8ms | ActiveRecord: 417.2ms)
The application is hosted on Heroku and if a request takes more than 30s it is killed. As you can see we are well above that limit. Most of the time is lost in rendering the view.
The page contains a date filter where the user gets to choose what Date Range to select the Orders from. So, caching is not the ideal solution since Date Ranges might change every time.
Any ideas how this can be done?
The Redis keys are of the format (The following is a Redis Hash):
orders:2012-01-01:123
orders:yyyy-mm-dd:$order-id
User simply provides a Date range and I get all the keys within that date range under the orders namespace.
Here’s how I would get the Customer Name for instance from the Redis order key:
= REDIS.hget(order_key, "customer_name")
Consider building the report with a periodic task using the Heroku Scheduler addon.
As long as last-minute orders are not required to be included in the report, you can build your reports nightly and have them available for immediate download to read with your morning coffee, or even have them mailed to you (or whoever needs to read them.)
If you need interactive selection of periods for reports, you will need to queue the requests up and build the reports using background jobs.