views:

48

answers:

2

I have a table with 10+ million rows. I need to create an index on a single column, however, the index takes so long to create that I get locks against the table.

It may be important to note that the index is being created as part of a 'rake db:migrate' step... I'm not adverse to creating the index manually if that will work.

UPDATE: I suppose I should have mentioned that this a write often table.

+1  A: 

you can prevent the blockage with something like this (pseudo-code):

create table temp like my_table;
update logger to log in temp;
alter table my_table add index new_index;
insert into my_table select * from temp;
update logger to log in my_table;
drop table temp

Where logger would be whatever adds rows/updates to your table in regular use(ex.: php script). This will set up a temporary table to use while the other one updates.

David Menard
I suppose I should have mentioned that this a write often table. So a temp table is going to be hard but not impossible to implement. Just a bit less than ideal.
Richard
A: 

Try to make sure that the index is created before the records are inserted. That way, the index will also be filled during the population of the table. Although that will take longer, at least it will be ready to go when the rake task is done.

Confusion
Most indexes are created this way; however; contrary to popular belief the wizards at Oracle and at Microsoft are constantly telling their DBAs to rebuild their indexes when performance starts to lag. Also in some ETL cases it's actually more efficient to index later.
Richard