Database Index Optimization and Migration Maintenance

Episode #53 by Teacher's Avatar David Kimura

Summary

Adding indexes to your databases can increase the SQL search performance on your tables. As your application grows, migration files can become out of hand, use squasher to squish them down to a single file.
rails database performance 4:54

Resources

Summary

# Terminal
gem install lol_dba
cd <RAILS_PROJECT> 
lol_dba db:find_indexes

* TIP: if you have a problem with the index name('index name too long') you can solve with the :name option. Something like :name => 'my_index'.

* run `rails g migration AddMissingIndexes` and add the following content:

    class AddMissingIndexes < ActiveRecord::Migration
      def change
        add_index :comments, :user_id
        add_index :comments, :post_id
        add_index :posts, :user_id
      end
    end

# Terminal
gem install squasher
squasher YYYY/MM/DD