LoginSignup
1
1

More than 5 years have passed since last update.

マイグレーションファイルに書いてある add_index

Posted at

【マイグレーションファイルに書いてある add_indexってなに】

特定のカラムからデータを取得する際に、テーブルの中の特定のカラムのデータを複製し検索が行いやすいようにしたものです。

indexに複数のカラムを設定する場合は
add_index テーブル名, [:カラム名1, :カラム名2, ・・]
と書けばOK

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.belongs_to :user, null: false
      t.date :recorded_on, null: false
      t.string :message
      t.timestamps
    end
    add_index :comments, [:user_id, :recorded_on]
  end
end

本来インデックスとは「索引」「見出し」などの意味をもつ言葉で、データベースに関わるシーンで用いられる際には、格納されたデータをより早く検索したり抽出できるように作られる索引データのことを言います。
SEO用語でインデックスと言えば、クローラーが収集したWEBページのデータを検索エンジンデータベースに(処理しやすいように整理された状態で)格納されることを「インデックス化」「インデクシング」などといいます。

https://qiita.com/seiya1121/items/fb074d727c6f40a55f22
https://www.seohacks.net/basic/terms/index/

1
1
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
1