This action might not be possible to undo. Are you sure you want to continue?
If you have space issues on a tablespace you can shrink tables and move indexes while online. MMON will send the alert for any tablespace if thresholds have been exceeded. To shrink table segments you can use the below commands, cascade will shrink both table and indexes(only these objects are affected), compact will stop before moving the high water mark (hwm) thus not reclaiming space back. , this may be useful as moving the hwm locks the table thus this may impact users. The benefits of a shrink operation are:
• • • •
Full table scans will take less time (a table scan will always scan upto the hwm even if space is not used) Better index access takes place because of a smaller b-tree Space is freed up for other database objects Space below the HWM is released and the HWM is moved down
Note: it is not guaranteed that all chained rows will be fixed because not all blocks may be read in a segment shrink operation. True statements of a shrink operation:
• • • • • • • •
Segment shrink is allowed only on segments whose space is automatically managed Heap-organised, index-organised and cluster tables can be shrunk ROW MOVEMENT must be enabled for heap-organised tables Chained rows may be repaired during a segment shrink operation Triggers are not fired during a segment shrink operation. Tables with ROWID-based materialized view are cannot be maintained IOT mapping tables and overflow segments cannot be shrunk Tables with function-based indexes cannot be shrunk
There are two phases in a segment shrink operation Compaction phase Adjustment of HWM (release free space) During compaction the rows are compacted and moved towards the left side of the segment, the HWM remains the same so the free space is still not available. DML is still available while the object is being compacted This hase is very short, oracle lowers the HWM and releases the free space. Oracle locks the object in an exclusive mode while the HMW is being lowered, meaning that no DML operations can take place.
A shrink could fail because the tablespace is not locally managed or do not have ASSM (automatic segment managed) enabled, the table has a column of long, row movement has not been enabled or it is a clustered table. There is no tool to display how often a segment is used but using the segment advisor in the OEM you can perform any of the below commands Determine amount of free space declare l_fs1_bytes number; l_fs2_bytes number; l_fs3_bytes number; l_fs4_bytes number;
l_fs1_blocks number; l_fs2_blocks number; l_fs3_blocks number; l_fs4_blocks number; l_full_bytes number; l_full_blocks number; l_unformatted_bytes number; l_unformatted_blocks number; bgin dbms_space.space_usage( segment_owner => user, segment_name => 'BOOKINGS', segment_type => 'TABLE', fs1_bytes => l_fs1_bytes, fs1_blocks => l_fs1_blocks, fs2_bytes => l_fs2_bytes, fs2_blocks => l_fs2_blocks, fs3_bytes => l_fs3_bytes, fs3_blocks => l_fs3_blocks, fs4_bytes => l_fs4_bytes, fs4_blocks => l_fs4_blocks, full_bytes => l_full_bytes, full_blocks => l_full_blocks, unformatted_blocks => l_unformatted_blocks, unformatted_bytes => l_unformatted_bytes ); dbms_output.put_line(' FS1 Blocks = '||l_fs1_blocks||' Bytes = '|| l_fs1_bytes); dbms_output.put_line(' FS2 Blocks = '||l_fs2_blocks||' Bytes = '|| l_fs2_bytes); dbms_output.put_line(' FS3 Blocks = '||l_fs3_blocks||' Bytes = '|| l_fs3_bytes); dbms_output.put_line(' FS4 Blocks = '||l_fs4_blocks||' Bytes = '|| l_fs4_bytes); dbms_output.put_line('Full Blocks = '||l_full_blocks||' Bytes = '|| l_full_bytes); end; / alter table <table> enable row movement; Enable row movement recover space from table and indexes and amend the HWM Note: required to perform a shrink operation alter table <table> shrink space cascade;
Note: this will lock the table recover space and don’t amend the HWM alter table <table> shrink space compact; recover space table and indexes and don’t alter table <table> shrink space compact cascade; amend the HWM alter table test03 deallocate unused; deallocate unused extents from table alter table test03 deallocate unsed keep 10M;
Shrinking index segments is required as when an index entry is deleted the space remains assigned. First you need to analyze the index and then query the index_stats view, remember the space is not removed but is available for reuse, to get the space back you must rebuild the index. Validate index analyze index test_indx validate structure; select lf_rows_len, del_lf_rows from index_stats where name =’IND1’; select pct_used from index_stats where name = ‘EMP_INDX’; Shrink index alter index test_indx shrink space; alter index emp_indx coalesce; Coalesce space Note: cannot move to another tablespace, does not require additional disk space, coalesces index leaf blocks with each branch alter index test_indx rebuild online; Note: uses journal table to store any new rows then applies to new index, can move index to another tablespace, requires double space to rebuild online, creates new tree and adjust tree height alter index test_indx monitoring usage; (turn on monitoring) alter index test_indx nomonitoring usage; (turn off monitoring) select index_name, table_name, monitoring, used, start_monitoring from v$object_usage where index_name = ‘TEST_IDX’;
Rebuild index (online)
Monitor index usage
dbms_advisor You can setup your own PL/SQL program to advise on tables, etc and to implement the recommendations. This can also be done under the EM console. create task create object set task parameter execute task delete task cancel task display task display recommendations Display actions dbms_advisor.create_task(‘Segment Advisor’, :task_id, task_name, ‘Free space in emp’, null); dbms_advisor_create_object( task_name, ‘TABLE’, ‘TEST01’, ‘EMP’, null, null, object_id); dbms_advisor.set_task_parameter( task_name, ‘recommend_all’, ‘true’); dbms_advisor.execute_task(task_name); dbms_advisor.delete_task(task_name); dbms_advisor.cancel_task(task_name); print task_id select owner, task_id, task_name, type, message, more_info from dba_advisor_findings where task_id = ??; select task_id, task_name, command, attrl from dba_advisor_actions where task_id = ???;
Alternative table storage structures The standard table type is a heap table, a heap consists of variable-length rows in random order, there is no structure to the physical storage. There are more advanced tables which can offer many benefits over the standard heap table. IOT Index organised table (IOT) is a B-Tree index but the leaf blocks contain key values followed by the
(index organized table) Index clusters
rest of the row, the saving is that only one segment is needed not two like a heap table and a index. For further details on creating, removing, etc see Oracle tables.doc An index cluster is a group of tables stored in one physical segment. All tables must be linked via a common key, it can be used to de-normalise tables in a foreign key relationship.
For further details on creating, removing, etc see Oracle tables.doc A hash cluster is created with a cluster key, as is a index cluster but rather than creating a index on this key oracle will use it to construct a hashing algorithm, oracle can calculate a rows location via this key. It is only useful if you search using the equality predicate on the key. Range search or get-nextrecord operations will almost certainly result in a full cluster scan. Don’t use hash clusters if you do Hash clusters not know how many rows will be returned. The new access path in a sorted hash cluster is used only if an equality predicate is used. For further details on creating, removing, etc see Oracle tables.doc Sorted hash clusters is the same as a hash cluster but you specify one or more columns to be used for ordering rows with the same cluster key. This makes sure that the rows are returned in the correct Sorted hash order. clusters For further details on creating, removing, etc see Oracle tables.doc Temporary tablespaces are used for order by, group by and create index. It is required when the system tablespace is locally managed. In oracle 10g you can now create temporary tablespace groups Temporary which means you can use multiple temporary tablespaces simultaneously. storage For further details on creating, removing, etc see tablespaces.doc
This action might not be possible to undo. Are you sure you want to continue?