Default indexes on id column?
I am creating a rails 3 ap开发者_如何学Pythonp on Heroku and it's using postgres as the database. In my rails migration I added indexes on all the ID columns. Was this a mistake? I'm starting to wonder if postgres has indexes by default on the ID columns.
If it was a mistake, how do I fix it? If I simply write a rails migration to remove the indexes from the ID columns will that fix everything?
According to postgresql's documentation:
Adding a primary key will automatically create a unique btree index on the column
or group of columns used in the primary key.
Hovewer, this does not prevent you from creating another index on the same columns. It might make sense if you use some different indexing policy (e.g. GiST index). But, if you are not sure about it, 99.9% that you have just created identical index.
Actually, this will not affect application functionality at all. The only thing to be concerned is that indexes are rebuilt on update operations, so it might pose some performance related issues. So, as manual suggests removing seldom-used indexes (last sentence), you'd better remove those indexes from db.
I'm not quite familiar with RoR migrations, but I think wriing a migration that remove those indexes is sufficient.
Check with \d "tablename" which indexes are present on your table. All of them are listed there, including the auto-created ones. If you find duplicates, it's easy enough to remove one (drop index "indexname" on "tablename"). You'll only remove the index with the according name ...
精彩评论