Updating denormalized database tables

后端 未结 2 1846
天命终不由人
天命终不由人 2021-01-17 00:20

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

2条回答
  •  悲&欢浪女
    2021-01-17 01:22

    There are a few ways to handle this situation:

    1. You can use a database trigger. This is not a database agnostic option and the RoR support of it is non-existent as far as I know. If your situation requires absolutely no data-inconsistency This would probably be the most performant way to achieve your goal, but I'm not a DB expert.
    2. You can use a batch operation to sync the two tables periodically. This method allows your two tables to drift apart and then re-synchronizes the data every so often. If your situation allows this drift to occur, this can a good option as it allows the DB to be updated during off hours. If you need to do the sync every 5 minutes you will probably want to look into other options. This can be handled by your ruby code, but will require a background job runner of some sort (cron, delayed_job, redis, etc.)
    3. You can use a callback from inside your Rails model. You can use "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.
    4. Some mechanism I haven't thought of....

    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.

提交回复
热议问题