Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - Adding columns to a table takes forever

I need to add two DATE columns to an already existing table. But it takes very long and I have to kill the process. What is strange is I was able to add those columns to the other tables in the same database momentarily. Most of those tables are larger than the one I'm having trouble with both data and column-count-wise. What do I have to do to be able to add those new columns?

like image 295
Mikayil Abdullayev Avatar asked May 12 '12 05:05

Mikayil Abdullayev


People also ask

How long does alter table add column take?

We have done migration of over 4 billion rows with this, though it can take upto 10 days, with less than a minute of downtime.

Can we add column to the existing table in Oracle?

Note that you cannot add a column that already exists in the table; trying to do so will cause an error. In addition, the ALTER TABLE ADD column statement adds the new column at the end of the table.

Which is fastest way to load data into Oracle?

SQL*Loader is the primary method for quickly populating Oracle tables with data from external files. It has a powerful data parsing engine that puts little limitation on the format of the data in the datafile. SQL*Loader is invoked when you specify the sqlldr command or use the Enterprise Manager interface.

Why is my Oracle database slow?

The most common causes of slow performance are as follows: Excessive round-trips from the application server to the database. Ideally, each UI operation should require exactly one round-trip to the database. Sometimes, the framework will require additional round-trips to retrieve and make session data persistent.


2 Answers

here's how I solved the problem. Before, I was specifying a default value for the columns right at the moment of adding them. But then I first added the columns with no default values. After columns got added I specified the default value and it executed immediately with no more waiting. Thank you very much @Justin Cave for your hint about Default value. That was the key point.

I have no doubt that it's related with the fact that when specifying default value at the time of adding column that default value is written to all the records inserted earlier.So if there are 5 million records in the table, that table will be updated to set default value for the newly added column for all the rows. Updating 5 million records is expensive as one might guess. But if default value is set after adding a column then the value of that new column in the rows inserted earlier will be NULL, so no update will take place.

like image 87
Mikayil Abdullayev Avatar answered Oct 14 '22 00:10

Mikayil Abdullayev


I was also facing this problem when adding a varchar2(4000) column with no default value one a 61K row table in Oracle 11g. After 30 minutes the column was still not added.

I tried to abort the operation, and retried it, but it still didn't finish.

What worked:
I tried adding a number column instead, that took a split second. Dropped it.
Then i tried adding a varchar2(1) column, that took a split second. Dropped it.
Then i tried adding a varchar2(4000) column again, and it only took a split second.

Silly "workaround", but maybe this also works for anyone else facing this problem.

like image 36
Wouter Avatar answered Oct 14 '22 00:10

Wouter