Ruby on rails 我应该在RubyonRails3.0中的计数器缓存列上添加索引吗
环境:Rails 3.0.4、MySQL、Ruby 1.8.7 我有下表:Ruby on rails 我应该在RubyonRails3.0中的计数器缓存列上添加索引吗,ruby-on-rails,ruby-on-rails-3,database-indexes,counter-cache,Ruby On Rails,Ruby On Rails 3,Database Indexes,Counter Cache,环境:Rails 3.0.4、MySQL、Ruby 1.8.7 我有下表: create_table "countries", :force => true do |t| t.string "iso", :limit => 2, :null => false t.string "name", :limit => 80, :null => f
create_table "countries", :force => true do |t|
t.string "iso", :limit => 2, :null => false
t.string "name", :limit => 80, :null => false
t.string "printable_name", :limit => 80, :null => false
t.string "iso3", :limit => 3
t.integer "numcode", :limit => 2
t.integer "users_count", :default => 0
t.integer "rank"
end
我定期搜索用户数超过n的国家/地区。将索引添加到计数器缓存“users\u count”有意义吗
我意识到它会为添加的每个用户增加一点开销,但我想确保计数器缓存机制中没有我遗漏的东西,正如您所说,添加索引会为写入
国家表的每个数据库增加一点开销,作为交换,可以加快使用该列的查询
根据经验,除非您的写入量非常大,否则如果您执行了大量引用查询的where
子句中users\u count
列的查询,则可能值得添加索引
Rails计数器缓存功能只是在创建所属国家的用户时增加用户数
值,在用户被销毁时减少该值