我有一个关于Rails数据库的问题。什么时候添加Rails中的表中的索引
- 我是否应该将“索引”添加到所有外键,如“xxx_id”?
- 我应该将“索引”添加到自动创建的“id”列吗?
我应该在自动创建的“id”列中添加“索引(唯一)”吗?
如果我一次(
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吧?
很好的回答。附加问题。我应该为xxx_id添加“唯一索引”,对吗? – 2010-09-08 01:17:52
不,请参阅我的更新回答 – jigfox 2010-09-08 07:04:07
问题,如果该字段很少显式搜索,您会索引外键吗? – Noz 2013-02-06 19:12:49