如何安全地从Rails 3架构中删除重复索引?

时间:2022-06-01 16:34:55

I'm working on a Rails 3 app, and we recently realized we have a duplicate index:

我正在开发一个Rails 3应用程序,我们最近意识到我们有一个重复的索引:

# from schema.rb
add_index "dogs", ["owner_id"], :name => "index_dogs_on_owner"
add_index "dogs", ["owner_id"], :name => "index_dogs_on_owner_id"

How can I check which index ActiveRecord is using for relevant queries? Or do I even need to? If one of the indices is removed will ActiveRecord happily just use the other?

如何检查ActiveRecord用于相关查询的索引?或者我甚至需要?如果其中一个索引被删除,ActiveRecord会愉快地使用另一个吗?

I can play around with it locally, but I'm not sure our production environment behaves exactly the same at the DB level.

我可以在本地使用它,但我不确定我们的生产环境在数据库级别上的行为是否完全相同。

1 个解决方案

#1


1  

The name of the index is arbitrary. The database engine will look at the indexes based on the column name, not the human name. The index will not affect ActiveRecord. I recommend removing whichever index is least obvious, in this case index_dogs_on_owner, because the other index is clearly on the owner_id column.

索引的名称是任意的。数据库引擎将根据列名而不是人名查看索引。索引不会影响ActiveRecord。我建议删除哪个索引最不明显,在本例中为index_dogs_on_owner,因为另一个索引显然在owner_id列上。

remove_index :dogs, :name => 'index_dogs_on_owner'

Cite: http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/remove_index

引用:http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/remove_index

#1


1  

The name of the index is arbitrary. The database engine will look at the indexes based on the column name, not the human name. The index will not affect ActiveRecord. I recommend removing whichever index is least obvious, in this case index_dogs_on_owner, because the other index is clearly on the owner_id column.

索引的名称是任意的。数据库引擎将根据列名而不是人名查看索引。索引不会影响ActiveRecord。我建议删除哪个索引最不明显,在本例中为index_dogs_on_owner,因为另一个索引显然在owner_id列上。

remove_index :dogs, :name => 'index_dogs_on_owner'

Cite: http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/remove_index

引用:http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/remove_index