Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我应该在RubyonRails3.0中的计数器缓存列上添加索引吗_Ruby On Rails_Ruby On Rails 3_Database Indexes_Counter Cache - Fatal编程技术网

Ruby on rails 我应该在RubyonRails3.0中的计数器缓存列上添加索引吗

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

环境: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 => 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计数器缓存功能只是在创建
所属国家的用户时增加
用户数
值,在用户被销毁时减少该值