【发布时间】:2013-11-24 06:49:58
【问题描述】:
我正在努力理解这段代码...它来自 Rails 教程手册,是制作类似 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 和 follow_id),为什么 他们需要索引吗?
- 索引是否以某种方式对它们进行排序?只是看起来有点奇怪 我要为有 2 列的表添加索引。
- 索引对行有什么作用?
- 索引是可选的吗?如果是这样,为什么/为什么不使用它?在上面的代码中使用它是不是一个好主意?
如果可以,请回答所有问题。我只是想弄清楚这个概念,在阅读后我有这些问题。
【问题讨论】:
标签: ruby-on-rails database indexing