I have a Rails 3.2 application that tracks mailings for subscription orders.
The basic model structure is:
Order has_many Subscriptions has_many SubscriptionMailings
Each month a record for each subscription mailing is generated and a csv file is exported from these records.
The mailing address is stored at the order level.
Basically I select all of the subscriptions that are valid to mail that month and loop through them getting the mailing address from the order object. Then I create a new subscription mailing record for each one.
Right now this works ok because there aren’t a lot of subscriptions, but it is pretty slow.
How can I speed up this process?
After some research I ended up wrapping my code in a transaction without making any other changes.
It sped up things quite a bit.
Before I added the transaction the code was taking over 1 minute to run, now it is down to roughly 10 seconds. This is plenty fast enough for my needs, so I didn’t try and optimize any further.