index_lifter

DESCRIPTION:

ActiveRecord utility class that disables database indexes during a block.

FEATURES/PROBLEMS:

  • Speeds up large database imports and large-scale data generation

  • Only use it in “maintenance mode”, i.e., when there are no other connections to the database.

  • If your application depends on unique indexes for working properly even during data generation, make sure they are not lifted.

SYNOPSIS:

desc "Populate the database with sample data"
task :populate => :environment do

  retained_indexes = [
    'index_people_on_lastname_and_firstname',
    { :table => :movies, :columns => :title }
    { :table => 'people', :columns => ['lastname', :firstname] }
  ]

  ActiveRecord::Base.transaction do
    IndexLifter.without_indexes(
      :movies,                       # Only consider indexes on these tables;
      :people,                       #   all tables by default.
      :except => retained_indexes,   # Don't lift these indexes
      :except_unique => true         # Don't lift unique indexes; default: false.
    ) do
      ActiveRecord::Base.silence do

        # import or generate large amounts of data here

      end
    end
  end
end

REQUIREMENTS:

  • ActiveRecord

INSTALL:

  • sudo gem install mschuerig-index_lifter

LICENSE:

(The MIT License)

Copyright © 2009 Michael Schuerig

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.