Skip to content

ActiveRecord schema command recreate_table for efficient changes to large tables

Notifications You must be signed in to change notification settings

til/recreate_table

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RecreateTable
=============

Applying schema changes and bulk updates to very large tables is often
done most efficiently by creating a new table with the desired
structure, copying the data into it and recreate the indices
afterwards.

RecreateTable offers commands to copy a table and rename it.

It only works with postgresql right now.


Example
=======

Imagine you have a table customers which contains many million rows,
and you want to add a rating column prefilled with the average from
the ratings table.

The traditional approach would look like this:

  class AddRatingToCustomers < ActiveRecord::Migration
  
    def self.up
      add_column :customers, :rating, :integer
      Customer.update_all(<<-EOSQL)
        rating=(SELECT AVG(rating) FROM ratings 
                 WHERE customer_id=customers.id)
      EOSQL
    end
    ...
  end


Using recreate_table you can migrate manually like this instead:


RecreateTable.prepare(:customers, :as => <<-EOSQL)
        SELECT *, (SELECT AVG(rating) FROM ratings 
                    WHERE customer_id=customers.id) AS rating
          FROM customers
      EOSQL


RecreateTable.activate


RecreateTable.cleanup



Notes
=====

The sql query given with the :as option is used to construct a
statement "CREATE new_table AS (...)".

It relies on many assumptions and may or may not work for you.

You can omit the :as option and simply use recreate_table to recreate
a table and all its indexes.

Updates to the original table after the recreate_table command started
to run will be lost. If that matters, make sure that there are no
updates e.g. by taking your app offline.

I am not sure whether the efficiency issue with bulk updates exists on
other databases as well. If yes, you could try to adapt it to
e.g. mysql, but it would require some effort to change.


Install
=======

cd myproject
script/plugin install git://github.com/til/recreate_table.git


License
=======

(The MIT License)

Copyright (c) 2009 Tilmann Singer <[email protected]>, released under the MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.





About

ActiveRecord schema command recreate_table for efficient changes to large tables

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages