Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default indexes on id column?

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?

like image 700
Brand Avatar asked Aug 21 '11 14:08

Brand


People also ask

Does identity column have index?

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.

Which index is by the default?

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.

Is primary key default index?

The primary key is the default clustered index in SQL Server and MySQL. This implies a 'clustered index penalty' on all non-clustered indexes.

Which index is created by default when a primary key is defined in a table?

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.


1 Answers

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.

like image 75
J0HN Avatar answered Sep 19 '22 07:09

J0HN