データベースにインデックスを追加
データベースにインデックスを追加したので、メモ。
rails g migration AddIndexToTable
※AddIndexToTableは任意のファイル名
\db\migrationに20130408??????_add_index_to_tableのように日付等が先頭の指定したファイル名のファイルができるので、このファイルにインデックスを追記する。
1つの列にインデックスを追加するとき
class AddIndexToTable < ActiveRecord::Migration def up add_index :tables, :column end def down remobe_index :tables, :column end end
2つの列に複合インデックスを追加するとき
class AddIndexToTable < ActiveRecord::Migration def up add_index :tables, [:column1, :column2], :name => :tables_index end def down remove_index :tables, :name => :tables_index end end
コマンドプロンプトで実行
rake db:migrate
参考:
新しいマイグレーションを追加してテーブルを変更 - Ruby on Rails入門
SQLite3にmigrationでadd_indexしても速くならない - 職業的思考内容