このコードについて理解しようとしています... これは Rails Tutorial Book からのもので、Twitter のようなアプリケーションを作成するプロセスの一部です。
class CreateRelationships < ActiveRecord::Migration
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id
t.timestamps
end
add_index :relationships, :follower_id
add_index :relationships, :followed_id
add_index :relationships, [:follower_id, :followed_id], unique: true
end
end
- 2 つの列 (follower_id と followed_id) しかないのに、なぜインデックスが必要なのですか?
- インデックスはそれらを何らかの方法でソートしますか? 2列のテーブルにインデックスを追加するのは少し奇妙に思えます。
- インデックスは行に対して何をしますか?
- 索引付けはオプションですか? もしそうなら、なぜ/なぜそれを使わないのですか? 上記のコードでそれを使用することは良い考えですか?
できればすべての質問に答えてください。私はこの概念に頭を悩ませようとしていますが、それについて読んだ後、これらの質問があります。