1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カラムにインデックスを貼ろう ❏Rails❏

Last updated at Posted at 2019-11-29

インデックスとは

カラムにインデックスを設定することで、データ検索を高速化させることができます!


使い方

マイグレーションファイルを作成

ターミナル
rails g migration AddIndexToTweets

マイグレーションファイルに、 `add_index :テーブル名, :カラム名`を追記
マイグレーションファイル
class AddIndexToTweets < ActiveRecord::Migration[5.2]
  def change
    add_index :tweets, :text
  end
end

複数ver.
マイグレーションファイル
class AddIndexToTweets < ActiveRecord::Migration[5.2]
  def change
    add_index :tweets, [:text, :title]
  end
end

# デメリットも

①データを保存・更新する速度が遅くなる
②データベースの容量を使う


インデックスの使い所は、 【格納するデータが多い時】 【データの検索が頻繁に行われる時】
ではまた!
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?