LoginSignup
5
2

More than 5 years have passed since last update.

[Rails] [小ネタ] インデックスの貼り忘れを教えてくれるRakeタスク

Last updated at Posted at 2017-09-07

インデックスを貼った方が良いカラムを一発で抽出する方法はないか探していると良さげなGemを見つけたので紹介します。

plentz/lol_dba

まずgemをインストールして、

$ gem install lol_dba

次のコマンドを実行すると・・・

$ 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 :users, :hoge_id
      end
    end

上記のように add_index :table_name, :column_id という形でインデックスを貼った方が良いものを出力してくれます!
あとは $ rails g migration AddMissingIndexes してコピペすればOKですね。

mysqlを使っているサービスで上記を実行したのですが、ソースをチラッと見た感じだと、mysql/sqlite/postgresのいずれでも大丈夫そうです。

ブログにも書いてます |д゚)チラッ
http://tackeyy.com/blog/posts/Introduction-of-lol_dba

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2