I am creating a rails 3 app 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?
If you create a column with a PRIMARY KEY constraint, a clustered index will be created by default (assuming this is a new table and no such index is already defined). Being an IDENTITY field has nothing to do with it.
A primary key index is created by default when a table is created with a primary key specified. It will match the primary key in nature, in that it will be a single-column index if the primary key is on a single column and a multi-column composite index if the primary key is a composite primary key.
The primary key is the default clustered index in SQL Server and MySQL. This implies a 'clustered index penalty' on all non-clustered indexes.
When a PRIMARY KEY constraint is defined, a clustered index will be created on the constraint columns by default, if there is no previous clustered index defined on that table.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With