I am using Ruby on Rails 3.0.7 and MySQL 5. In my application I have two database tables, say TABLE1 and TABLE2, and for performance reasons I have denormalizated some data
Or you can maintain normalized set of data and have your two denomalized tables. And periodically sync them. Other way have a normalized table structure to maintain data (insert/update/delete) and write a materialized view to do the reporting, that is what you are achieving by unnormalized view. you can set data updation parameters for materialized views as per your requirements.
There are a few ways to handle this situation:
"after_update :sync_denormalized_data"
. This callback will be wrapped in a database level transaction (assuming your database supports transactions). You will have Rails level code, consistent data, and no need for a background process at the expense of making two writes every time.These types of issues are very application specific. Even within the same application you may use more than one of the methods depending on the flexibility and performance requirements involved.