This tutorial describes how to use the segment advisor to proactively manage space in your database.
Approximately 20 minutes
This tutorial covers the following topics:
| Overview | ||
| Prerequisites | ||
| Creating and Generating Data Activity in a Table | ||
| Reclaiming Space by Using the Segment Advisor | ||
| Summary | ||
Place the cursor over this icon to load and view all
the screenshots for this tutorial. (Caution: Because this action loads all screenshots
simultaneously, response time may be slow depending on your Internet connection.)
Note: Alternatively, you can place the cursor over each individual icon in the following steps to load and view only the screenshot associated with that step.
When space used by an object at steady state is much less than the amount of space allocated to it, it becomes necessary to reclaim the unused space. With previous releases of the Oracle server, space once allocated below the segment's high-water mark (HWM) could be freed only by moving or redefining the segment. In Oracle Database 10g, you can reorganize and shrink segments. When a segment is shrunk, its data is compacted, its HWM is pushed down, and unused space is released back to the tablespace containing the segment. A shrink operation is an online and in-place operation, and does not use extra database space to be executed. In-place means the shrink operation does not create a separate object but rather that the operation is done on the same object.
Oracle Database 10g Release 1 provides a Segment Advisor that helps to identify segments that are good candidates for shrinking based on the amount of free space available in them. In Oracle 10g Release 2, this capability is enhanced by introducing the Automatic Segment Advisor, which proactively runs the Segment Advisor on segments that are likely to need space monitoring (based on system activity as well as space pressures on the tablespace where these segments reside). This feature minimizes the need to run Segment Advisor manually. If the segment that you are considering for shrinking has not already been analyzed by the Automatic Segment Advisor, run the Segment Advisor manually on these segments.
In this tutorial, you create a table, generate activity, and then use the Segment Advisor to determine whether space can be reclaimed.
Before starting this tutorial, you should::
| 1. | Perform the Installing Oracle Database 10g on Windows tutorial. |
|
| 2. | Download and unzip the space.zip into your working directory (c:\wkdir) |
|
To demonstrate the Segment Advisor, you 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. |
Open your browser and enter the following URL (Replace <hostname> with your own host name or IP address): http://<hostname>:1158/em Enter sys as User Name, oracle as Password, SYSDBA in the Connect As field, and click Login.
|
| 2. |
Click the Administration tab.
|
| 3. |
Click Tables in the Schema section.
|
| 4. |
Click Create.
|
| 5. | Select Standard, Heap Organized and click Continue.
|
| 6. |
Enter EMPLOYEES1 in the Name field. Specify SYSTEM as the schema and EXAMPLE as the tablespace. Click the Define Using drop-down list and select SQL. Enter select * from hr.employees in the CREATE TABLE AS field, and then click the Options tab.
|
| 7. |
Select Yes for Enable Row Movement and click OK to complete the creation of the table.
|
| 8. |
Your table has been created. Click the Database Instance locator link.
|
| 9. |
To start a SQL*Plus session, select Start > Programs > Oracle - OraDb10g_home1 > Application Development > SQL Plus. Enter system as the User Name, oracle as Password and click OK.
|
| 10. |
Execute the following commands 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;
/
|
| 11. |
To simulate more user activity on the EMPLOYEES1 table, execute the following SQL script in your SQL*Plus session: @c:\wkdir\genactivity02 The genactivity02.sql
file includes the following commands: 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;
/
|
| 12. |
To simulate even more user activity on the EMPLOYEES1 table, execute the following SQL script in your SQL*Plus session: @c:\wkdir\genactivity03 The genactivity03
file includes the following commands: begin
for i in 1..500 loop
insert into system.employees1
select * from hr.employees;
commit;
end loop;
end;
/
|
| 13. |
To simulate more user activity on the EMPLOYEES1 table, execute the following SQL script in your SQL*Plus session: @c:\wkdir\genactivity04 The genactivity04.sql
file includes the following commands: 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;
|
| 14. |
Switch back to Enterprise Manager. Now, you can take a look at the table segment statistics. From the Administration tab, click the Tables link.
|
| 15. |
To locate the SYSTEM.EMPLOYEES1 table, enter SYSTEM in the Schema field and emp in the Object Name field, and then click Go.
|
| 16. |
Click Edit.
|
| 17. |
Click the Segments tab.
|
| 18. |
Note the percentage of wasted space in the EMPLOYEES1 table. (Your actual values may differ from the ones in this screenshot.) You can use the Segment Advisor to reclaim the unused space in this table. Click the Database Instance locator link.
|
Perform the following steps:
| 1. |
Scroll down to the bottom of the page. Click the Advisor Central link in the Related Links section.
Note: In Oracle Database 10g Release 2, the Segment Advisor automatically runs at various intervals. As a result, you see the number of Segment Advisor Recommendations represented on the Home page after a period of time.
|
| 2. |
Click Segment Advisor.
|
| 3. |
Select Schema Objects and click Next.
|
| 4. |
Click Add.
|
| 5. |
Select Table for Type, enter system as Schema and employees1 as Object, then click Search.
|
| 6. |
In the Results section, select the SYSTEM.EMPLOYEES1 table and click OK.
|
| 7. |
Click Next.
|
| 8. |
In the Schedule window, ensure that the Schedule Type is Standard and Start is Immediately. Click Next.
|
| 9. |
Click Submit.
|
| 10. | Click Refresh.
|
| 11. | Select SEGMENTADV_<number>, the result for the request you have just submitted and click View Result.
|
| 12. |
Click Recommendation Details.
|
| 13. |
Select the EMPLOYEES1 segment and click Shrink in the Recommendation column. (Your values may differ slightly from the ones on the screenshot).
|
| 14. |
Click Implement.
|
| 15. |
Click Submit.
|
| 16. |
Now you can check how much space was actually freed from the table. Click the Database Instance locator link.
|
| 17. |
Click the Tables on the Administration page.
|
| 18. |
Enter SYSTEM as Schema and EMP as Object Name and click Go.
|
| 19. |
Select EMPLOYEES1 and click Edit.
|
| 20. |
Click the Segments tab.
|
| 21. |
The wasted space has been reduced significantly. (Your actual values may be different from the ones displayed in this screenshot).
|
| 22. |
Return to your SQL*Plus session and execute the following SQL script : @c:\wkdir\cleanup The cleanup.sql file includes the following commands: drop table system.employees1 purge; exit
|
In this tutorial, you learned how to reclaim space in a table by using the Segment Advisor.
Place the cursor on this icon to hide all screenshots.