managing schema objects
DESCRIPTION
Managing Schema Objects. Objectives. After completing this lesson, you should be able to do the following: Configure automatic segment-space management Estimate table and index size Perform online redefinition of tables Enable automatic statistics collection Use the Segment Advisor - PowerPoint PPT PresentationTRANSCRIPT
16Copyright © 2004, Oracle. All rights reserved.
Managing Schema Objects
16-2 Copyright © 2004, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do the following:
• Configure automatic segment-space management
• Estimate table and index size
• Perform online redefinition of tables
• Enable automatic statistics collection
• Use the Segment Advisor
• Enable resumable space allocation
16-3 Copyright © 2004, Oracle. All rights reserved.
Using Automatic Segment-Space Management
• Bitmaps are used to manage the free space within segments
• Benefits provided by this capability include:– Ease of use– Better space utilization– Better concurrency handling– Better performance
16-4 Copyright © 2004, Oracle. All rights reserved.
Automatic Segment-Space Managementat Work
Extent
BMB
BMB BMB
BMB BMB
DATA
BMB
BMB
BMB BMB BMB
{SEGMENT
Block
… … … …
…
…
…
…
…
…
…BMB
BMB
16-6 Copyright © 2004, Oracle. All rights reserved.
Creating an Automatic Segment-SpaceManagement Segment
• Segments are declared at the tablespace level.• Tablespace must be permanent and locally
managed.• SEGMENT SPACE MANAGEMENT is the attribute used
for tablespace creation, which cannot be subsequently altered.
• Automatic space management segments are specified with the AUTO keyword.
• For free list segments, use the default value of MANUAL.
• Specifications of PCTUSED, FREELIST, and FREELIST GROUPS are ignored at table creation.
16-7 Copyright © 2004, Oracle. All rights reserved.
Using Enterprise Manager to Specify Automatic Segment-Space Management
16-8 Copyright © 2004, Oracle. All rights reserved.
Using SQL to Create an Automatic Space Management Segment
SQL> CREATE TABLESPACE sample 2 EXTENT MANAGEMENT LOCAL 3 SEGMENT SPACE MANAGEMENT AUTO;
SQL> CREATE TABLE students 2 (name VARCHAR2(30), hobbies CLOB) 3 TABLESPACE sample;
16-9 Copyright © 2004, Oracle. All rights reserved.
Granting Object Privileges on Behalf of the Object Owner
• GRANT ANY OBJECT PRIVILEGE system privilege allows you to grant and revoke any object privilege on behalf of the object owner.
• You can grant access to objects in any schema without connecting to the schema.
• Part of the Oracle-supplied DBA role
• When you use this privilege to grant a privilege, the object owner is defined as the grantor, unless you have the object privilege you are granting.
16-11 Copyright © 2004, Oracle. All rights reserved.
Online Redefinition of Tables
• Occasionally, you need to reorganize a large heavily used table.
• Previously, these redefinitions forced the table to be unavailable for the duration of the operation.
• Table redefinitions can be performed online beginning with Oracle9i.
16-12 Copyright © 2004, Oracle. All rights reserved.
Online Table Redefinition: Features
• A nonpartitioned table can be converted into a partitioned table and vice versa.
• The organization of a table can be changed from a heap-based to index-organized table (IOT), and vice versa.
• Columns can be dropped.
• New columns can be added to a table.
• Parallel support can be added or removed.
• Storage parameters can be modified.
• A column can be renamed.
• A table can be moved to a new tablespace.
16-13 Copyright © 2004, Oracle. All rights reserved.
Sourcetable
Online Table Redefinition
Transformupdates Store DML
changes
Track DML operations
Resulttable
Transform
16-14 Copyright © 2004, Oracle. All rights reserved.
Using the DBMS_REDEFITION Package
1. Decide on the method of redefinition.
2. Determine if the table is a candidate for online redefinition with the CAN_REDEF_TABLE procedure.
3. Create an empty interim table with the desired characteristics.
4. Start the redefinition process with the START_REDEF_TABLE procedure.
5. Use the COPY_TABLE_DEPENDENTS procedure to automatically create dependent objects.
6. Finish the redefinition process with the FINISH_REDEF_TABLE procedure.
16-16 Copyright © 2004, Oracle. All rights reserved.
Online Table Redefinition: Synchronizing the Interim Table
• Use the SYNC_INTERIM_TABLE procedure to periodically synchronize the interim table with the original one.
• Synchronization is recommended if there is significant DML activity between the start and finish of the redefinition.
16-17 Copyright © 2004, Oracle. All rights reserved.
Online Table Redefinition: Terminating the Redefinition Process
• Use the ABORT_REDEF_TABLE procedure to terminate the redefinition.
• Use if an error occurs during the process.
• After this procedure executes, drop the interim table and its associated objects.
16-18 Copyright © 2004, Oracle. All rights reserved.
Online Table Redefinition: Limitations
• If you are using the primary key (or pseudo-primary key) method, the table to be redefined must have the same primary key or pseudo-primary key columns.
• You cannot use the rowid method of redefinition for index-organized tables (IOTs).
• The following are not supported: – User-defined data types– BFILE columns– Tables with materialized view logs– Horizontal subsetting, vertical subsetting, and
column transformations
16-19 Copyright © 2004, Oracle. All rights reserved.
Online Table Redefinition: Limitations
• LONG columns must be converted to CLOBS; LONG RAW columns must be converted to BLOBS.
• New columns being added must not be declared as NOT NULL until the redefinition is complete.
• A subset of rows cannot be redefined.
• The following tables cannot be redefined:– Tables in the SYS or SYSTEM schema– An overflow table of an IOT– Materialized view container tables– Advanced queuing tables– Temporary tables– Clustered tables
16-20 Copyright © 2004, Oracle. All rights reserved.
Resumable Space Allocation: Overview
Resumable space allocation provides:
• The ability to suspend and resume execution of large database operations in the event of repairable failure
• Support for errors related to space limits and out-of-space conditions
• An opportunity for the DBA to take corrective steps to resolve the error condition
• Suspended statements that automatically continue operation
16-21 Copyright © 2004, Oracle. All rights reserved.
Resumable Space Allocation: Life Cycle
• Resumable space allocation is enabled using the ALTER SESSION command.
• A statement is suspended when one of the following conditions occurs:– Out of space condition– Maximum number of extents reached condition– Space quota exceeded condition
• When a statement is suspended:– The error is reported in the alert log– A system event trigger, after suspension, can be
executed
• When the error condition disappears, the suspended statement automatically resumes.
16-23 Copyright © 2004, Oracle. All rights reserved.
Resumable Space Allocation: Operations
• Queries: SELECT statements that run out of temporary space.
• Data manipulation language commands: INSERT, UPDATE, DELETE
• Import/Export when invoked with the resumable space allocation option.
• SQL*Loader when invoked with the resumable space allocation option.
• Various data definition language commands
16-24 Copyright © 2004, Oracle. All rights reserved.
Enabling Resumable Space Allocation Systemwide
• Enable with the RESUMABLE_TIMEOUT initialization parameter
• Specify a timeout interval with the RESUMABLE_TIMEOUT initialization parameter
• Disabled by default
• Can be changed dynamically
• Within a session, users can enable resumable space allocation.
16-25 Copyright © 2004, Oracle. All rights reserved.
• Enable resumable space allocation:
• Disable resumable space allocation:
• Change the session name:
• Change the timeout period:
Enabling Session Resumable Space Allocation
SQL> ALTER SESSION ENABLE RESUMABLE 2 TIMEOUT 600 NAME 'Starting Point';
SQL> ALTER SESSION DISABLE RESUMABLE;
SQL> ALTER SESSION ENABLE RESUMABLE NAME 2 'new name';
SQL> ALTER SESSION ENABLE RESUMABLE TIMEOUT 2 3600;
16-27 Copyright © 2004, Oracle. All rights reserved.
Using the DBMS_RESUMABLE Package
DBMS_RESUMABLE procedures:
• ABORT(sessionID)• GET_SESSION_TIMEOUT(sessionID)• SET_SESSION_TIMEOUT(sessionID,timeout)• GET_TIMEOUT()• SET_TIMEOUT(timeout)• SPACE_ERROR_INFO(error_type,
object_type,object_owner, table_space_name,object_name, sub_object_name)
16-29 Copyright © 2004, Oracle. All rights reserved.
Creating an AFTER SUSPEND System Event
• Automatically generated when a statement encounters a correctable error:
• SQL statements that are executed in an AFTER SUSPEND trigger are always nonresumable.
CREATE OR REPLACE TRIGGER res_defaultafter suspend on databaseDECLARE BEGIN /* send an email to notify DBA */ COMMIT; END;
16-31 Copyright © 2004, Oracle. All rights reserved.
Obtaining Information About the Statusof Resumable Statements
SESSION_ID Session identifier of the statement
INSTANCE_ID Instance number of the statement
SQL_TEXT First 1,000 characters of the statement
NAME The name given to the statement
STATUS RUNNING, SUSPENDED, COMPLETED,ABORTED, TIMEOUT
ERROR_NUMBER Error code of the last correctable error (1)
ERROR_MSG Error message corresponding to (1)
START_TIME Start time of the statement
SUSPEND_TIME Last time the statement was suspended
RESUME_TIME Last time the statement was resumed
DBA_RESUMABLE dictionary view:
16-33 Copyright © 2004, Oracle. All rights reserved.
Data Segment Compression: Overview
• Applies to heap-organized tables only
• Compresses data inside blocks
• Useful for data warehouse environment:– Disk use reduction – Memory use reduction– Query execution speedup– Limited update activity– Optimized for direct-load scenarios
16-34 Copyright © 2004, Oracle. All rights reserved.
Data Segment Compression at Work
188670 C 563.7188670 I 1648.7188670 P 571.95188670 S 140.25188670 T 633.35189450 Z 5055.5189450 U 2714.6189450 A 4199.1189450 K 6296.9189450 R 94.65
188670 189450
C 563.7, I 1648.7, P 571.95, S 140.25, T 633.35, Z 5055.5, U 2714.6, A 4199.1, K 6296.9, R 94.65
Blockbefore compression
Blockafter compression
16-35 Copyright © 2004, Oracle. All rights reserved.
Creating Compressed Segments
• You can compress:– Entire tables– Specific partitions of a partitioned table
• Use the new COMPRESS attribute at:– Tablespace level– Table level– Partition level
• Ideal for rolling window operations
16-36 Copyright © 2004, Oracle. All rights reserved.
Creating Compressed Segments: Example
CREATE TABLE customers_comp COMPRESS ASSELECT * FROM sh.customers;
CREATE TABLESPACE sample DATAFILE 'sample01.dbf' SIZE 20MDEFAULT COMPRESS;
CREATE TABLE costs( prod_id,time_id,unit_cost,unit_price)PARTITION BY RANGE (time_id) ( PARTITION c2001 VALUES LESS THAN ('2002') TABLESPACE SAMPLE, PARTITION c2002 VALUES LESS THAN ('2003') TABLESPACE SAMPLE NOCOMPRESS);
16-37 Copyright © 2004, Oracle. All rights reserved.
Compressing Segments
• Immediately move a segment to a compressed or noncompressed form:
• Change a segment’s compressed scheme for future utilization:
• Operations are not possible for partitioned tables having bitmap indexes (ORA-14646).
ALTER TABLE customers MOVE COMPRESS;
ALTER TABLE customers COMPRESS;
16-38 Copyright © 2004, Oracle. All rights reserved.
Achieving a Better Compression Ratio
• Find one row ordering with the maximum number of repeated values per block.
• With only one low-cardinality column:
• Low cardinality can be determined by querying:– DBA_TAB_COL_STATISTICS– DBA_PART_COL_STATISTICS– DBA_SUBPART_COL_STATISTICS
CREATE TABLE compressed COMPRESS ASSELECT * FROM to_be_compressedORDER BY low_cardinality_column;
16-39 Copyright © 2004, Oracle. All rights reserved.
Achieving a Better Compression Ratio
• In presence of multiple low cardinality columns:– Determine the C1 column with the lowest cardinality– Determine the C2 column with the lowest cardinality
of other columns for fixed values of C1– Determine the C3 column with the lowest cardinality
when the first two are fixed– By continuing this process, you can determine
some column sequence C1, C2, …, Cn
• Create the table using the sequence found:
CREATE TABLE compressed COMPRESS ASSELECT * FROM to_be_compressedORDER BY C1, C2, …, Cn;
16-41 Copyright © 2004, Oracle. All rights reserved.
Determining Whether a Table Is Using Compression
• For partitioned tables:– COMPRESSION column inside
DBA_TAB_PARTITIONS, or DBA_TAB_SUBPARTITOINS
– DEF_COMPRESSION inside DBA_PART_TABLES
• For a particular segment: SELECT d.segment_name, d.partition_name, DECODE(BITAND(s.spare1,2048),2048, 'ENABLED', 'DISABLED')FROM sys.seg$ s, dba_segments d, sys.ts$ tWHERE d.HEADER_FILE = s.FILE# and d.HEADER_BLOCK = s.BLOCK# and t.ts# = s.ts# and t.name = 'SYSTEM' and d.segment_name='TPART' and d.owner='SYSTEM';
16-42 Copyright © 2004, Oracle. All rights reserved.
Segment Advisor: Overview
• Determines the list of objects that are good candidates for shrinking
• Recommendations are based on:– Sampled analysis– Historical information– Future growth trends
• Accessible from Database Control:– Advisor Central page– Tablespaces page– Schema object pages
16-43 Copyright © 2004, Oracle. All rights reserved.
Invoking the Segment Advisor
16-44 Copyright © 2004, Oracle. All rights reserved.
Viewing the Growth Trend Report
• Used by the Segment Advisor
• Space usage statistics are collected in the AWR.
16-45 Copyright © 2004, Oracle. All rights reserved.
Segment Resource Estimation
16-46 Copyright © 2004, Oracle. All rights reserved.
Shrinking Segments: Overview
HWM
HWM
Shrinkoperation
DataUnusedspace
Data
Unusedspace
Reclaimed space
16-47 Copyright © 2004, Oracle. All rights reserved.
Shrinking Segments: Considerations
• Online and in-place operation
• Applicable only to segments residing in ASSM tablespaces
• Candidate segment types:– Heap-organized tables and index-organized tables– Indexes– Partitions and subpartitions– Materialized views and materialized view logs
• Indexes are maintained.
• Triggers are not fired.
16-48 Copyright © 2004, Oracle. All rights reserved.
Shrinking Segments by Using SQL
ALTER TABLE employees SHRINK SPACE CASCADE;
ALTER … SHRINK SPACE [CASCADE]
TABLE INDEX MATERIALIZED VIEW MATERIALIZED VIEW LOG
MODIFY PARTITION
ALTER TABLE employees ENABLE ROW MOVEMENT; 1
2
MODIFY SUBPARTITION
16-49 Copyright © 2004, Oracle. All rights reserved.
Segment Shrink: Basic Execution
HWM
ALTER TABLE employees SHRINK SPACE COMPACT;
HWM
HWM
ALTER TABLE employees SHRINK SPACE;
1
2
16-50 Copyright © 2004, Oracle. All rights reserved.
Segment Shrink:Execution Considerations
• Use compaction only:– To avoid unnecessary cursor invalidation– During peak hours
• DML operations and queries can be issued during compaction.
• DML operations are blocked when HWM is adjusted.
16-51 Copyright © 2004, Oracle. All rights reserved.
Using EM to Shrink Segments
16-52 Copyright © 2004, Oracle. All rights reserved.
Migrating to the Cost-Based Optimizer
• Oracle Database 10g supports only the cost-based optimizer.
• The cost-based optimizer relies on accurate statistics to determine the optimal access path for a query.
• Plan stability can be maintained by using stored outlines to capture, save, and reuse the execution plans for all of the queries of a given application.
16-53 Copyright © 2004, Oracle. All rights reserved.
Automatic Optimizer Statistics Collection: Overview
• Oracle8i provides the DBMS_STATS package:– DBA determines how to gather statistics.– DBA determines when to gather statistics.
• Oracle9i determines how to gather statistics:– Statistics can be gathered using a single command.– DBA determines when to gather statistics.
• Oracle Database 10g fully automates statistics gathering:– DBA no longer has to gather statistics.– Table monitoring is used by default.
16-54 Copyright © 2004, Oracle. All rights reserved.
Automatically Collecting Statistics
• STATISTICS_LEVEL = TYPICAL | ALL• Statistics gathered by the predefined
GATHER_STATS_JOB job
• This job implicitly determines:– Database objects with missing or stale statistics– Appropriate sampling percentage necessary to
gather good statistics on those objects– Appropriate columns that require histograms and
the size of those histograms– Degree of parallelism for statistics gathering– Prioritization of objects on which to collect
statistics
16-55 Copyright © 2004, Oracle. All rights reserved.
GATHER_STATS_JOB
AUTO_TASKS_JOB_CLASS
AUTO_TASKS_CONSUMER_GROUP
GATHER_STATS_JOB
WEEKNIGHT_WINDOW
10 p.m.–6 a.m. Mon to Fri
MAINTENANCE_WINDOW_GROUP
WEEKEND_WINDOW
12 a.m. Sat to 12 a.m. Mon
STATISTICS_LEVEL = TYPICAL or ALL
16-56 Copyright © 2004, Oracle. All rights reserved.
Changing the GATHER_STATS_JOB Schedule
16-57 Copyright © 2004, Oracle. All rights reserved.
Locking Statistics
• Prevents automatic gathering
• Mainly used for volatile tables:– Lock without statistics implies dynamic sampling– Lock with statistics for representative values
EXECUTE DBMS_STATS.LOCK_TABLE_STATS ('owner name', 'table name');
EXECUTE DBMS_STATS.LOCK_SCHEMA_STATS ('owner name');
SELECT stattype_lockedFROM dba_tab_statistics;
16-58 Copyright © 2004, Oracle. All rights reserved.
Using the DBMS_STATS Package
• New FORCE argument
• Override statistics locking
EXECUTE DBMS_STATS.DELETE_*_STATS(…,- FORCE=>TRUE);
EXECUTE DBMS_STATS.IMPORT_*_STATS(…,-FORCE=>TRUE);
EXECUTE DBMS_STATS.SET_*_STATS(…,-FORCE=>TRUE);
EXECUTE DBMS_STATS.RESTORE_*_STATS(…,- FORCE=>TRUE);
16-59 Copyright © 2004, Oracle. All rights reserved.
Automatic Statistics Collection: Considerations
You should continue to gather statistics manually in the following cases:
• After bulk operations
• When using external tables
• To collect system statistics
• To collect statistics on fixed objects
16-60 Copyright © 2004, Oracle. All rights reserved.
Life Cycle of Optimizer Statistics
GATHER_STATS IMPORT_STATS SET_STATS
DBA_TAB_STATS_HISTORY
Newstatistics
Oldstatistics
DBA_OPTSTATS_OPERATIONS
31days
RESTORE_TABLE_STATS
Operationdate
16-61 Copyright © 2004, Oracle. All rights reserved.
Managing Historical Optimizer Statistics
exec dbms_stats.restore_table_stats(-<owner name>, <table name>, timestamp);
exec dbms_stats.purge_stats(<beforetime>);
exec dbms_stats.alter_stats_history_retention(-<retention>);
DECLARE r number; a timestamp;BEGINa:=dbms_stats.get_stats_history_availability;r:=dbms_stats.get_stats_history_retention;dbms_output.put_line(a||' -- '||r);end;
16-63 Copyright © 2004, Oracle. All rights reserved.
Monitoring Index Usage
• Start index usage monitoring:
• Stop index usage monitoring:
• Query V$OBJECT_USAGE for the index being monitored to determine whether the index has been used.
ALTER INDEX index MONITORING USAGE;
ALTER INDEX index NOMONITORING USAGE;
16-64 Copyright © 2004, Oracle. All rights reserved.
Enabling FORCE LOGGING
• Use the new [NO]FORCE LOGGING option of the ALTER DATABASE command.
• NO FORCE is the default.
• FORCE LOGGING forces redo records to be written when NOLOGGING operations are executed.
• This is required for a “no data loss” strategy.
• FORCE_LOGGING column of V$DATABASE displays mode: YES or NO.
SQL> ALTER DATABASE FORCE LOGGING;
16-65 Copyright © 2004, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Configure automatic segment-space management
• Estimate table and index size
• Perform online redefinition of tables
• Enable automatic statistics collection
• Use the Segment Advisor
• Enable resumable space allocation
16-66 Copyright © 2004, Oracle. All rights reserved.
Practice 16 Overview: Managing Schema Objects
This practice covers the following topics:
• Proactively managing your database using ADDM
• Using resumable space allocation and Import
• Using the Segment Advisor