Ruby on rails 为Rails中的现有表生成索引

Ruby on rails 为Rails中的现有表生成索引,ruby-on-rails,ruby,postgresql,activerecord,Ruby On Rails,Ruby,Postgresql,Activerecord,我如何在RubyonRails应用程序中将索引(通过现有数据构建索引)重新索引到表中 我已创建以下迁移文件: class AddIndexProductImagesOnProductId < ActiveRecord::Migration def change add_index :product_images, :product_id end end 下面的索引确实添加到了schema.rb中 add_index "product_images", ["produc

我如何在RubyonRails应用程序中将索引(通过现有数据构建索引)重新索引到表中

我已创建以下迁移文件:

class AddIndexProductImagesOnProductId < ActiveRecord::Migration
  def change
    add_index :product_images, :product_id
  end
end
下面的索引确实添加到了schema.rb中

  add_index "product_images", ["product_id"], name: "index_product_images_on_product_id", using: :btree
但现有数据仍然未编入索引,只有新添加的数据正在编入索引


如何索引该表的现有数据?(我正在使用Postgres)

如何证明现有数据没有被索引?查询仍然非常慢(性能相同),Postgresql Studio显示“索引扫描=5”。从1开始,现在只有5行了。我在表中添加了行。@GuyDubrovski索引不是这样工作的。所有的值都被编入索引(空值除外),所以你有一个“为什么postgresql不使用我的索引”的问题。谢谢@davidridge,你知道postgres为什么不使用索引吗/让我们在这里提供您的查询,可能查询没有按预期使用您的索引。顺便说一句,您可以使用newrelic监视数据库性能
  add_index "product_images", ["product_id"], name: "index_product_images_on_product_id", using: :btree