Rebuilding Database Objects Online

This tutorial describes how to perform various rebuilding and reorganizing tasks on your database.

Approximately 30 minutes

This tutorial covers the following topics:

Place the cursor over this icon to load and view all the screenshots for this tutorial. (Caution: This action loads all screenshots simultaneously, so response time may be slow depending on your Internet connection.)

Note: Alternatively, you can place the cursor over an individual icon in the following steps to load and view only the screenshot associated with that step. You can hide an individual screenshot by clicking it.

Oracle Online Redefinition allows changes to be made to tables while users are reading and updating the same tables. This features provides for continuity of service because there is no impact on the end-users. When the reorganization is complete, the switch to the new reorganized table is automatic and completely transparent to the application users.

Back to Topic List

Before starting this tutorial, you should:

1.

Perform the Installing Oracle Database 10g on Windows tutorial.

2.

Download and unzip the onlredef.zip file into your working directory (i.e. c:\wkdir).

 

In this section, you reorganize the EMPLOYEES table in the HR schema. You:

Partition the table in new tablespaces EMP_TBS1 and EMP_TBS2

Perform the following steps:

1.

Open a browser and launch Enterprise Manager as follows:

http://<your host name>:1158/em

Login as system/oracle and click Login.

Note: You may receive the license agreement page. If so, scroll down to the bottom of the page and click I agree.

 

4.

Select the Administration tab.

 

5.

Scroll down to the Schema section.

 

6.

Click on the Tables link.

 

7.

Enter HR in the Schema field and EMP in the Object Name field. Then click Go.

 

8.

Select the EMPLOYEES table link.

 

9.

Notice that the EMPLOYEES table is not partitioned and it does not have a column named LOCATION. Click OK.

 

10.

In order to redefine your table, you need to verify that the table is a candidate for online redefinition and specify that redefinition is to be performed using a primary key. Log in to SQL*Plus as system/oracle and execute the redef_emp_cand script as follows:

@c:\wkdir\redef_emp_cand

BEGIN
DBMS_REDEFINITION.CAN_REDEF_TABLE('HR','employees',
dbms_redefinition.cons_use_pk);
END;
/

 

11.

You now create two tablespaces and an interim table. The interim table shows what the table will look like after the reorganization, including the LOCATION_ID column and the partitioning scheme. In your SQL*Plus session, execute the redef_crobj script as follows:

@c:\wkdir\redef_crobj

connect system/oracle
CREATE TABLESPACE EMP_TBS1;
CREATE TABLESPACE EMP_TBS2;
CREATE TABLE hr.int_employees
(employee_id NUMBER(6) ,
first_name VARCHAR2(20) ,
last_name VARCHAR2(25) ,
email VARCHAR2(25) ,
phone_number VARCHAR2(20) ,
location NUMBER(4),
hire_date DATE DEFAULT (sysdate),
job_id VARCHAR2(10),
salary NUMBER(8,2),
commission_pct NUMBER (2) DEFAULT(10),
manager_id NUMBER(6),
department_id NUMBER(4) )
PARTITION BY RANGE(employee_id)
(PARTITION emp1000 VALUES LESS THAN (1000) TABLESPACE EMP_TBS1,
PARTITION emp2000 VALUES LESS THAN (2000) TABLESPACE EMP_TBS2);

 

12.

Now you can start the redefinition process and define the column mappings for the old and new columns as well as increase the commission by 1%. In your SQL*Plus session, execute the redef_startscript as follows:

@c:\wkdir\redef_start

connect system/oracle
BEGIN
DBMS_REDEFINITION.START_REDEF_TABLE('hr', 'employees','int_employees',
'employee_id employee_id, first_name first_name, last_name last_name, email email, phone_number phone_number, 0 location, hire_date hire_date, job_id job_id, salary salary, commission_pct+1 commission_pct, manager_id manager_id, department_id department_id' , dbms_redefinition.cons_use_pk);
END;
/

 

13.

Start another SQL*Plus session where you can make changes to the EMPLOYEES table. Connect as system/oracle and execute the redef_testscript as follows:

@c:\wkdir\redef_test

connect hr/hr
UPDATE employees set salary = 25000 where employee_id=200;

Note: Do not commit this transaction at this time.

 

14.

Next you automatically create any triggers, indexes and constraints on the interim table. Switch back to the first SQL*Plus session and execute the redef_crdep_obj script:

@c:\wkdir\redef_crdep_obj

connect system/oracle
set serveroutput on
DECLARE
no_errors int;
BEGIN
DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS ('hr', 'employees','int_employees',1, TRUE, TRUE, TRUE, FALSE, no_errors);
DBMS_OUTPUT.put_line ('No of errors during copy of dependents ' || no_errors );
END;
/

 

15.

Return to your second SQL*Plus session (where you executed the redef_test script) and commit the salary change. Exit from this SQL*Plus session.

commit;
exit

 

16.

Now you synchronize the interim table so that it includes the salary change that you made. Return to your first SQL*Plus session and execute the redef_sync script:

@c:\wkdir\redef_sync

connect system/oracle
BEGIN
DBMS_REDEFINITION.SYNC_INTERIM_TABLE('hr', 'employees', 'int_employees');
END;
/

 

17.

Now you finish the reorganization and the tables will be transparently switched. In your SQL*Plus session, execute the redef_finish_drop script:

@c:\wkdir\redef_finish_drop

connect system/oracle
BEGIN
DBMS_REDEFINITION.FINISH_REDEF_TABLE('hr', 'employees', 'int_employees');
END;
/
DROP TABLE hr.int_employees CASCADE CONSTRAINTS PURGE;

 

18.

Now you view the reorganized table in Enterprise Manager. Switch back to your Enterprise Manager session. Click the Refresh button in your browser and click EMPLOYEES again.

 

19.

Notice the new LOCATION column. Scroll down to the bottom.

 

20.

The table is also now partitioned. Click OK.

 

Another type of reorganization that you may have to perform is recovering space of segments. This involves compacting the segments to recover the space and then making the space available. This task is performed online. In this tutorial you will create and generate data activity in a table. This table has its Enable Row Movement option set to Yes. Selecting this option allows for space reclamation on this table. Perform the following steps:

1.

On the Tables page in Enterprise Manager, click the Create button.

Move your mouse over this icon to see the image

 

2.

Accept the default of "Standard, Heap Organized" and click Continue.

Move your mouse over this icon to see the image

 

3.

Enter EMPLOYEES1 in the Name field. Specify SYSTEM as the schema and EXAMPLE as the tablespace. Select SQL in the Define Using drop-down list. Enter select * from hr.employees in the Create Table As field. Click the Options tab.

Move your mouse over this icon to see the image

 

4.

Select Yes in the Enable Row Movement list and click OK to complete the creation of the table.

Move your mouse over this icon to see the image

 

5.

Your table has been created.

Move your mouse over this icon to see the image

 

6.

Now you generate some activity. Log in to SQL*Plus as system/oracle. Execute the genactivity01 script to simulate user activity on the EMPLOYEES1 table.

@c:\wkdir\genactivity01

The genactivity01.sql file includes the following:

begin
  for i in 1..1000 loop
    insert into system.employees1
    select * from hr.employees;
    commit;
  end loop;
end;
/
          

Move your mouse over this icon to see the image

 

7.

To simulate more user activity on the EMPLOYEES1 table, execute the genactivity02 script in your SQL*Plus session:

@c:\wkdir\genactivity02

The genactivity02.sql file includes the following:

delete system.employees1 where department_id = 50;
begin
  for i in 1..500 loop
    insert into system.employees1
    select * from hr.employees;
    commit;
  end loop;
end;
/
           

Move your mouse over this icon to see the image

 

8.

To simulate even more user activity on the EMPLOYEES1 table, execute the genactivity03.sql script in your SQL*Plus session:

@c:\wkdir\genactivity03.sql

The genactivity03.sql file includes the following:

begin
  for i in 1..500 loop
     insert into system.employees1
     select * from hr.employees;
     commit;
  end loop;
end;
/

Move your mouse over this icon to see the image

 

9.

To simulate more user activity on the EMPLOYEES1 table, execute the genactivity04 script in your SQL*Plus session:

@c:\wkdirgenactivity04

The genactivity04.sql file includes the following:

delete system.employees1 where department_id = 30;
commit;
delete system.employees1 where department_id = 100;
commit;
delete system.employees1 where department_id = 50;
commit;
delete system.employees1 where department_id = 80;
commit;

Move your mouse over this icon to see the image

 

10.

Return to your Enterprise Manager Tables page. Select the EMPLOYEES1 table and click Edit.

Move your mouse over this icon to see the image

 

11.

Click the Segments tab.

Move your mouse over this icon to see the image

 

12.

Note the percentage of wasted space in the EMPLOYEES1 table. You can use the Segment Advisor to reclaim the unused space in this table. Select Shrink Segment from the Actions list and click Go.

Move your mouse over this icon to see the image

 

13.

Click Continue.

Move your mouse over this icon to see the image

 

14.

On the Schedule page, enter Segment_Shrink_Employees1 for the Job Name. Ensure that Immediately is selected and click Submit.

Move your mouse over this icon to see the image

 

15.

Your job has been submitted. Click the All tab.

Move your mouse over this icon to see the image

 

16.

Your segment shrink job was successful. Now you review the statistics. Click your Database Instance breadcrumb.

Move your mouse over this icon to see the image

 

17.

Scroll down the Administration page. Click the Tables link in the Database Objects section.

Move your mouse over this icon to see the image

 

18.

Enter SYSTEM for Schema and EMP for Object Name. Click Go.

Move your mouse over this icon to see the image

 

19.

Make sure the EMPLOYEES1 table is selected and click Edit.

Move your mouse over this icon to see the image

 

20.

Click the Segments tab.

Move your mouse over this icon to see the image

 

21.

Notice that the Wasted Space(%) has been drastically reduced. Select the Database Instance breadcrumb.

Move your mouse over this icon to see the image

 

Rebuilding indexes is another task that DBAs typically perform. In this tutorial, you will rebuild the index for JHIST_JOB_IS online and move it into a new tablespace called INDEXES. Perform the following steps:

1.

Log in to SQL*Plus as system/oracle and execute the rebuild_crtblsp script to create a new tablespace:

@c:\wkdir\rebuild_crtblsp

connect system/oracle
CREATE TABLESPACE indexes;

 

2.

Return to your Enterprise Manager window. Select Indexes in the Database Objects section of the Administration page.

 

3.

Select Index Name in the Search By list. Enter HR in the Schema field and JHIST% in the Object Name field. Click Go.

 

4.

Note that the JHIST_JOB_IX index is currently stored in the EXAMPLE tablespace.

 

5.

In your SQL*Plus session, issue the following command to move the index to the INDEXES tablespace you just created:

ALTER INDEX hr.jhist_job_ix REBUILD ONLINE TABLESPACE indexes; 

 

6.

Return to your Enterprise Manager session and refresh the page. You see that the index is now assigned to the INDEXES tablespace. Click the Database breadcrumb to return to the Administration page.

 

There are times when storage attributes of a table must be changed. Perform the following steps:

1.

Scroll down the Administration page.

 

2.

Under Database Objects, select Reorganize Objects.

 

3.

Make sure Schema Objects is selected and click Next.

 

4.

Click Add.

 

5.

Select Tables in the Type list and enter HR for the Schema. Then click Search.

 

6.

In the Available Objects: Tables section, select HR.DEPARTMENTS and click OK.

 

7.

Click Set Attributes.

 

8.

Select Relocate object to another tablespace and enter EMP_TBS2 for the tablespace name. Click the Storage tab.

 

9.

Enter 2MB for the Initial Size of the Extents. Then click OK.

 

10.

Click Next.

 

11.

In the Method section, select Availability (online). Click Next.

 

12.

The script was generated and an impact report produced with no errors. Click Next.

 

13.

Enter your host credential username and password. Click Next.

 

14.

The script to be executed is displayed. Click Submit Job .

 

15.

The job was created. Click the job link to view the results.

 

16.

Click the Status link.

 

17.

Click the Reorganize link.

 

18.

The output is shown. The reorganize operation completed successfully. Scroll to the bottom of the page to review the entire output log. Click Database to return to the Database home page.

 

In this tutorial, you learned how to:

Change a table definition
Perform an online segment shrink
Rebuild an index
Change a table storage attribute

Back to Topic List

Move your mouse over this icon to hide all screenshots.