Large table to add a field needs

Test objectives:

   Customer demand, the next set 11.2.0.4 environment, 4G large table, add a field.

   From this test, time consuming, application-level locks, and whether to add a default field values, real memory test;

 

Testing process:

  A. Create a test table

SQL> drop table a purge;

Table dropped.

SQL> create table a as select * from dba_objects;

Table created.

SQL> set timing on
多次循环插入
 
 

SQL> insert into a select * from a;
22291968 rows created.
SQL> commit;
SQL> select sum(Bytes)/1024/1024 from user_segments where segment_name='A' ;
SUM(BYTES)/1024/1024
--------------------
4990

II. Add fields, and the additional default

1) application lock resource 
Session 1

SQL> delete a where rownum=1;

1 row deleted.

Session 2
The SQL> ALTER ddl_lock_timeout the session SET = 600; 
the SQL> ALTER Table A the Add C_LHR VARCHAR2 (100) the DEFAULT 'of LHR';
session hang live lock resource query application

SQL> select object_id from dba_objects where owner='YZ' and object_name='A';

OBJECT_ID
----------
89526

SQL> select sid,id1,type,lmode,request,ctime,block from v$lock where id1=89526;

SID ID1 TY LMODE REQUEST CTIME BLOCK
---------- ---------- -- ---------- ---------- ---------- ----------
329 89526 TM 3 0 156 1
312 89526 TM 0 6 86 0   申请TM 6!!!

 

2) Update Time

Session 1 rollback, so that the operation is performed automatically add the field

SQL> roll;
Rollback complete.

And performance-related!  

event query the user is performing

SQL> select event,sid from gv$session where status='ACTIVE' and username='YZ';

EVENT SID
----------------------------------------------------------------- ------
log file switch (checkpoint incomplete) 312
SQL*Net message from client 329

 

Because the test environment log is not formatted, and therefore the log file is very small, less the number of log group, eventually leading to the Session is CKPT process and wait for the result of unfinished greatly delay time, and a large number of archive logs generated.

Query statistics execution alter table operation session event statistics

Elapsed: 00:40:59.26

 

 

 

Can be found in the database waiting CKPT process is complete write for 20 minutes, waiting for log switch 15 minutes, the opposite seems the largest number of physical units fast read times up to 40 million times, but the actual time spent only three minutes.

This does not involve optimization of, but from the point of view of this modification, plus default value directly add field, poor efficiency, high cost, number lock TM6 lock table.

 

III. Add fields, but without default, set default values ​​for fields subsequent

      The comparison is not rigorous in the field has been added, add at times (but test results can reflect on the line)

1), and observed the application resource lock
Session 1

SQL> delete a where rownum=1;

1 row deleted.

Session 2
SQL> alter session set ddl_lock_timeout=600;
SQL>  alter table a add C_LHR_NEW VARCHAR2(100);
Live session hang, check the application's resource lock

SQL> select object_id from dba_objects where owner='YZ' and object_name='A';

OBJECT_ID
----------
89526

SQL> select sid,id1,type,lmode,request,ctime,block from v$lock where id1=89526;

SID ID1 TY LMODE REQUEST CTIME BLOCK
------ ---------- -- ---------- ---------- ---------- ----------
312 89526 OD 6 0 10 0
329 89526 TM 3 0 30 0
312 89526 TM 3 0 10 0

No blocked phenomenon, query test environment blocked Session, the application can be found in a lock resource TX

SQL> select sid,id1,type,lmode,request,ctime,block from v$lock where REQUEST>0;

SID ID1 TY LMODE REQUEST CTIME BLOCK
------ ---------- -- ---------- ---------- ---------- ----------
312 131075 TX 0 4 74 0

Can be found not on the table to add TM> 3 level lock does not lock the table, but there is a business table DML operations can not add fields. 

2) observation time
session1 rollback
the SQL> ROLLBACK;

Session2 second results, i.e., does not modify the data row, just modified record information related to the data dictionary.

Query data

SQL> set autotrace TRACE
SQL> select * from a where rownum=1;

Predicate Information (identified by operation id):
---------------------------------------------------

1 - filter(ROWNUM=1)

 

  3) increase in new columns of the table, increasing the default value

SQL> ALTER TABLE a MODIFY C_LHR_NEW VARCHAR2(100) DEFAULT 'LHR';

Table altered.

Elapsed: 00: 00: 00.04 seconds out of results

Query data

SQL> select C_LHR_NEW from a where rownum=1;

C_LHR_NEW
----------------------------------------------------------------------------------------------------

Null

SQL>alter table a modify C_LHR_NEW not null
*
ERROR at line 1:
ORA-02296: cannot enable (YZ.) - null values found

4) In other words, add fields, modify the default values, not null modify the properties can not be operated because of null values in the column 
still yet another

SQL> alter table a add C_LHR_NEW2 varchar2(200) default 'LHR' not null;

Table altered.

Elapsed: 00: 00: 00.04 seconds out of

SQL> select C_LHR,C_LHR_NEW,C_LHR_NEW2 FROM A where rownum=1;

C_LHR C_LHR_NEW C_LHR_NEW2
---------- ---------- --------------------
LHR                  LHR

 

5) Using the above method, the data query, if the real store data or other data stored in the manner

Create a test table

SQL> create table b as select * from dba_objects ;

SQL> alter table b add c_name varchar2(100) default 'LHR' not null;        

SQL> select count(*) from b where c_name='CC';

COUNT(*)
----------
0

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
SQL_ID 9qj24brgbnrkg, child number 0
-------------------------------------
select count(*) from b where c_name='CC'

Plan hash value: 749587668

---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 347 (100)| |
| 1 | SORT AGGREGATE | | 1 | 52 | | |

|* 2 | TABLE ACCESS FULL| B | 14 | 728 | 347 (1)| 00:00:05 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - filter(NVL("C_NAME",'LHR')='CC')    

That is, for oracle columns created in this way, does not actually store data, but displays the query parameters using nvl when no data is null value, LHR default data value is displayed

 

SQL> update b set c_name='DD' where object_id=20;

SQL> commit;

 

SQL> select count(*) from b where c_name='DD';

COUNT(*)
----------
1

   2 - filter(NVL("C_NAME",'LHR')='DD')

 

Guess you like

Origin www.cnblogs.com/lvcha001/p/11837569.html