rails 给数据库表里加入索引

创建迁移文件

rails g migration add_product_images_index

编写迁移文件

db/migrate/20170119093958_add_product_images_index.rb

class AddProductImagesIndex < ActiveRecord::Migration[5.0]
  def change
    add_index :product_images, [:product_id, :weight] #创建联合索引
  end
end

执行迁移

rake db:migrate

[Haima的博客] http://www.cnblogs.com/haima/
原文地址:https://www.cnblogs.com/haima/p/15033101.html