我正在通过类似以下代码获取记录
@community = Community.find_by_community_name(params[:community_name])
@user = User.find_by_username(params[:username])
我希望让它加载得更快,所以我考虑像这样为它们添加索引。
如果我执行rake db:migrate
,它会重新为现有记录建立索引吗?
还是只建立从现在开始创建的记录的索引?
像这样添加索引是否可以提高加载速度?
class AddIndexToCommunity < ActiveRecord::Migration
def self.up
add_index :communities, [:community_name, :title, :body], :name=>:communities_idx
end
def self.down
remove_index :communities, [:community_name, :title, :body], :name=>:communities_idx
end
end
class AddIndexToUser < ActiveRecord::Migration
def self.up
add_index :users, [:username, :nickname, :body], :name=>:users_idx
end
def self.down
remove_index :users, [:username, :nickname, :body], :name=>:users_idx
end
end
tags
和taggings
,并且taggings
具有tag_id
属性。我应该为tag.name
和tagging.tag_id
添加索引,因为它可以更快地获取与标签相关的记录吗? - MKK