Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insertion of data after creating index on empty table or creating unique index after inserting data on oracle?

Which option is better and faster? Insertion of data after creating index on empty table or creating unique index after inserting data. I have around 10M rows to insert. Which option would be better so that I could have least downtime.

like image 840
DKSRathore Avatar asked Dec 31 '09 04:12

DKSRathore


People also ask

Should I CREATE INDEX before or after insert?

You should create an index for a table after inserting or loading data (via SQL*Loader or Import) into the table. It is more efficient to insert rows of data into a table that has no indexes and then create the indexes for subsequent access.

What happens when you create an index on a table?

Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries. Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update).

What is the difference between CREATE INDEX and create unique index?

Index: It is a schema object which is used to provide improved performance in the retrieval of rows from a table. Unique Index: Unique indexes guarantee that no two rows of a table have duplicate values in the key column (or columns).

Do indexed columns need to be unique?

The column you want to index does not need to have unique values.


2 Answers

Insert your data first, then create your index.

Every time you do an UPDATE, INSERT or DELETE operation, any indexes on the table have to be updated as well. So if you create the index first, and then insert 10M rows, the index will have to be updated 10M times as well (unless you're doing bulk operations).

like image 187
womp Avatar answered Oct 03 '22 18:10

womp


It is faster and better to insert the records and then create the index after rows have been imported. It's faster because you don't have the overhead of index maintenance as the rows are inserted and it is better from a fragmentation standpoint on your indexes.

Obviously for a unique index, be sure that the data you are importing is unique so you don't have failures when trying to create the index.

like image 45
RC. Avatar answered Oct 03 '22 18:10

RC.