我对Rails数据库有疑问。
- 我应该在所有外键(例如“ xxx_id”)上添加“ index”吗?
- 我应该在自动创建的“ id”列中添加“ index”吗?
我应该在自动创建的“ id”列中添加“ index(unique)”吗?
如果我一次将索引添加到两个外键(
add_index (:users, [:category, :state_id])
,会发生什么呢?与为每个键添加索引有何不同?class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.integer :category_id t.integer :state_id t.string :email t.boolean :activated t.timestamps end # Do I need this? Is it meaningless to add the index to the primary key? # If so, do I need :unique => true ? add_index :users, :id # I don't think I need ":unique => true here", right? add_index :users, :category_id # Should I need this? add_index :users, :state_id # Should I need this? # Are the above the same as the following? add_index (:users, [:category, :state_id]) end end
到目前为止,很好的答案。附加问题。
- 我应该为xxx_id添加“唯一索引”,对吧?