Skip to content

Instantly share code, notes, and snippets.

@yn-misaki
Last active November 29, 2016 02:16
Show Gist options
  • Save yn-misaki/374242bad0843fddc4c9b9bb6bddb5c6 to your computer and use it in GitHub Desktop.
Save yn-misaki/374242bad0843fddc4c9b9bb6bddb5c6 to your computer and use it in GitHub Desktop.
【MySQL5.6以上】Webエンジニア向け!メンテなしで500万件レコード入りのテーブルにINDEXを張る実行時間の目安 ref: http://qiita.com/yn-misaki/items/018a76d15d1fe96fd9d7
ALTER TABLE テーブル名 ADD INDEX インデックス名(カラム名);
/** 自動採番のプライマリーキーは、NULLにしましょう(エラーで怒られなくて済みます) **/
INSERT INTO テーブル名
SELECT NULL(プライマリキー),カラム名1,...,カラムn FROM テーブル名;
$ bundle exec rake db:migrate RAILS_ENV=development
$ bundle exec rake db:migrate RAILS_ENV=production
==================================================
== 20161128017930 AddIndexToHoge: migrating
-- add_index(:companies, [:foo_number, :bar_id, :bar_status], {:name=>"index_hoge_on_foo_number_and_bar_id_and_bar_status"})
-> 52.6168s
== 20161128017930 AddIndexToHoge: migrated (52.6169s)
==================================================
class AddIndexToHoge < ActiveRecord::Migration
def change
# テーブル:hoge
# カラム: foo_id, bar_id, bar_status
# 単体のINDEX
# add_index :hoge, :bar_status
# 複合INDEXを作成
add_index :hoge, [:foo_number, :bar_id, :bar_status]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment