Home » SQL & PL/SQL » SQL & PL/SQL » Adding column to the table is taking long time (oracle 10g , solaris)
Adding column to the table is taking long time [message #603864] Fri, 20 December 2013 01:32 Go to next message
prejib
Messages: 126
Registered: March 2009
Location: India
Senior Member
Hi,
I am adding one new column to the existing table having 71 Millions of records. But the DDL is taking long time to add the column . I am not giving any default value for the column ..Please suggest how can we reduce the timing ? Is there any way to calculate the time taken to add a column to the table in advance?

Thanks ,
prejib
Re: Adding column to the table is taking long time [message #603866 is a reply to message #603864] Fri, 20 December 2013 01:50 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
Well...making room for one more column in 71 million rows will take some time I guess.

Maybe some parralellisation could help you?
(btw...what is a lomg time?)
Re: Adding column to the table is taking long time [message #603867 is a reply to message #603864] Fri, 20 December 2013 01:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Adding a column with no default value is instantaneous.
Copy and paste what you did, including the previous table description.

[Updated on: Fri, 20 December 2013 01:52]

Report message to a moderator

Re: Adding column to the table is taking long time [message #603868 is a reply to message #603867] Fri, 20 December 2013 01:55 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
Really?
I have to do some reading up I guess.

I would say all rows in all blocks must get the notion that there is a new column (even if it's null)

Well I dive into the documentation Smile
Re: Adding column to the table is taking long time [message #603869 is a reply to message #603868] Fri, 20 December 2013 02:01 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
Okay....I see.

Only the meta-data will be altered, when the default is null.
However this is since Oracle 11g.

So .... I'll hide behind "the OP didn't supply a version" Smile

Anyway....tnx for pointing this out.
Re: Adding column to the table is taking long time [message #603870 is a reply to message #603869] Fri, 20 December 2013 02:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

But he did supply it "(oracle 10g , solaris)". Wink

Quote:
However this is since Oracle 11g.


As far as I know, it was the case in all versions at least up to 11.2 (I haven't 12c).
Re: Adding column to the table is taking long time [message #603871 is a reply to message #603870] Fri, 20 December 2013 02:29 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
Michel Cadot wrote on Fri, 20 December 2013 09:16

But he did supply it "(oracle 10g , solaris)". Wink

I searched the screen....and yes...there is it.
I often fail to see that part of the information....my bad.

Quote:

Quote:
However this is since Oracle 11g.


As far as I know, it was the case in all versions at least up to 11.2 (I haven't 12c).

well as you understand my research took at most 5 minutes, and I allready found your statement to be true. However all resources (about 3) mentioned that this ("deferred creation") happens since 11g.

Somewhere today I will look it up Smile


Anyway...thanks for the knowledge
Re: Adding column to the table is taking long time [message #603872 is a reply to message #603870] Fri, 20 December 2013 02:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Here's a test case done in 8i:
SQL> create table t as select object_id from dba_objects;

Table created.

SQL> alter session set events '10046 trace name context forever, level 1';

Session altered.

SQL> alter table t add owner varchar2(30);

Table altered.

SQL> alter session set events '10046 trace name context off';

Session altered.

SQL> @v

Version Oracle : 8.1.7.4.1

And the trace file is attached -> only dictionary accesses, all statements are (long lines split by me):
alter session set events '10046 trace name context forever, level 1'
alter table t add owner varchar2(30)
delete from idl_ub1$ where obj#=:1
delete from idl_char$ where obj#=:1
delete from idl_ub2$ where obj#=:1
delete from idl_sb4$ where obj#=:1
delete from error$ where obj#=:1
select o.owner#, u.name,        o.name,            o.namespace,         o.obj#,   d.d_timestamp, nvl(d.property,0)  
  from dependency$ d, obj$ o, user$ u  where d.p_obj#=:1  and   (d.p_timestamp=:2 or d.property=2)
  and   d.d_obj#=o.obj#  and   o.owner#=u.user#  and decode(:3,0,0,o.type#)=:3
update tab$ set ts#=:2,file#=:3,block#=:4,bobj#=decode(:5,0,null,:5),tab#=decode(:6,0,null,:6),intcols=:7,kernelcols=:8,
  clucols=decode(:9,0,null,:9),audit$=:10,flags=:11,pctfree$=:12,pctused$=:13,initrans=:14,maxtrans=:15,rowcnt=:16,
  blkcnt=:17,empcnt=:18,avgspc=:19,chncnt=:20,avgrln=:21,analyzetime=:22,samplesize=:23,cols=:24,property=:25,
  degree=decode(:26,1,null,:26),instances=decode(:27,1,null,:27),dataobj#=:28,avgspc_flb=:29,flbcnt=:30,trigflag=:31,
  spare1=:32,spare2=decode(:33,0,null,:33),spare4=:34,spare3=:35 where obj#=:1insert into   
  col$(obj#,name,intcol#,segcol#,type#,length,precision#,scale,null$,offset,fixedstorage,segcollength,deflength,default$,
  col#,property,charsetid,charsetform,spare1,spare2)values(:1,:2,:3,:4,:5,:6,decode(:7,0,null,:7),
  decode(:5,2,decode(:8,-127/*MAXSB1MINAL*/,null,:8),178,:8,179,:8,180,:8,181,:8,182,:8,183,:8,231,:8,null),:9,0,:10,:11,
  decode(:12,0,null,:12),:13,:14,:15,:16,:17,:18,:19)
update obj$ set obj#=:6,type#=:7,ctime=:8,mtime=:9,stime=:10,status=:11,dataobj#=:13,flags=:14,oid$=:15,spare1=:16 
  where owner#=:1 and name=:2 and namespace=:3 and(remoteowner=:4 or remoteowner is null and :4 is null)and(linkname=:5 
  or linkname is null and :5 is null)and(subname=:12 or subname is null and :12 is null)
alter session set events '10046 trace name context off'

  • Attachment: ORA06072.TRC
    (Size: 7.05KB, Downloaded 1053 times)
Re: Adding column to the table is taking long time [message #603874 is a reply to message #603871] Fri, 20 December 2013 02:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

I think "deferred creation" refers to defer segment creation at table creation time (which comes with 11g) not deferred segment update when column is added (with no default value).
The reason is that, column is added at the end of the column list and last null columns are not physically present in the blocks.

There is an exception about this: when you have a LONG column in the table as the LONG column must be the last one in the block (so physically not necessary in the column list).

Re: Adding column to the table is taking long time [message #603875 is a reply to message #603871] Fri, 20 December 2013 02:41 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
martijn wrote on Fri, 20 December 2013 09:29
g.
Somewhere today I will look it up Smile


From the documentation (alter table)

Oracle Documentation
When you add a column, the initial value of each row for the new column is null.

If you specify the DEFAULT clause for a NOT NULL column, then the default value is stored as metadata but the column itself is not populated with data. However, subsequent queries that specify the new column are rewritten so that the default value is returned in the result set.

This optimized behavior differs from earlier releases, when as part of the ALTER TABLE operation Oracle Database updated each row in the newly created column with the default value, and then fired any update triggers defined on the table. In this release, no triggers are fired because the default is stored only as metadata. The optimized behavior is subject to the following restrictions:



In the documentation of Oracle 10 I can not find a remark concerning "deferred column creation" (alter table)

So I assume that creating the new column will take the OP some time.
Re: Adding column to the table is taking long time [message #603877 is a reply to message #603875] Fri, 20 December 2013 02:45 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Oh yes, you are referring to a column creation with default value, here we are talking about adding a column without a default value.

Re: Adding column to the table is taking long time [message #603878 is a reply to message #603864] Fri, 20 December 2013 02:48 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
prejib wrote on Fri, 20 December 2013 13:02
But the DDL is taking long time to add the column .


It is absurd to just say "it takes long time". There is no information provided by you to support what you say. Have a look at this thread.
Re: Adding column to the table is taking long time [message #603919 is a reply to message #603878] Fri, 20 December 2013 05:32 Go to previous messageGo to next message
prejib
Messages: 126
Registered: March 2009
Location: India
Senior Member
Thanks for your replies.
Still I don't understand why it took around half an hour for this activity . I have droped the column and added the same again , it took very few seconds.
Re: Adding column to the table is taking long time [message #603920 is a reply to message #603919] Fri, 20 December 2013 05:37 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
prejib wrote on Fri, 20 December 2013 11:32
Thanks for your replies.
Still I don't understand why it took around half an hour for this activity . I have droped the column and added the same again , it took very few seconds.

It sounds like there was something blocking the action rather than the action itself taking a long time.
Re: Adding column to the table is taking long time [message #603946 is a reply to message #603919] Fri, 20 December 2013 10:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
In addition to pablolee's answer, when you experiment such waits there are few things to first do and enable you to diagnose most of the problems:

A/ query v$session to see the state of the session, the most important columns are (from Reference Manual):

Quote:
BLOCKING_SESSION NUMBER Session identifier of the blocking session
WAIT_CLASS VARCHAR2(64) Name of the class of the wait event
WAIT_TIME NUMBER If the session is currently waiting, then the value is 0. If the session is not in a wait, then the value is as follows:
> 0 - Value is the duration of the last wait in hundredths of a second

-1 - Duration of the last wait was less than a hundredth of a second

-2 - Parameter TIMED_STATISTICS was set to false

This column has been deprecated in favor of the columns WAIT_TIME_MICRO and STATE.

SECONDS_IN_WAIT NUMBER If the session is currently waiting, then the value is the amount of time waited for the current wait. If the session is not in a wait, then the value is the amount of time since the start of the last wait.
This column has been deprecated in favor of the columns WAIT_TIME_MICRO and TIME_SINCE_LAST_WAIT_MICRO.

STATE VARCHAR2(19) Wait state:
WAITING - Session is currently waiting

WAITED UNKNOWN TIME - Duration of the last wait is unknown; this is the value when the parameter TIMED_STATISTICS is set to false

WAITED SHORT TIME - Last wait was less than a hundredth of a second

WAITED KNOWN TIME - Duration of the last wait is specified in the WAIT_TIME column


B/ if the wait is a lock then query v$lock

C/ if the wait is not a lock then have a look et the following columns in v$session

Quote:
SEQ# NUMBER A number that uniquely identifies the current or last wait (incremented for each wait)
EVENT# NUMBER Event number
EVENT VARCHAR2(64) Resource or event for which the session is waiting. See Also: Appendix C, "Oracle Wait Events"


Query it several times if event changes or event stays the same but seq# increase then you are waiting for an operation to complete with short waits for each action (read file, read temp...) of this operation; the event will tell you what are these actions (refer to the Appendix C for more information about the events).

D/ if you want more details about what happens, activate a trace on the session (see DBMS_MONITOR procedure).



Re: Adding column to the table is taking long time [message #604049 is a reply to message #603946] Mon, 23 December 2013 06:13 Go to previous message
pointers
Messages: 451
Registered: May 2008
Senior Member
Hi,

[url=http://www.proligence.com/pres/sangam13/beginning_performance_tuning.pdf]

Regards,
pointers
Previous Topic: getting error on enabling constraints even using novalidate clause
Next Topic: Explain below query
Goto Forum:
  


Current Time: Tue Aug 19 20:03:08 CDT 2025