390 likes | 536 Vues
This chapter delves into advanced table management techniques in Oracle 9i, focusing on Large Object (LOB) columns and index-organized tables. Learn how to create tables with LOB columns, understanding storage options and the types of LOBs (internal and external). Discover the use of new packages for LOB management, the semantics of copying LOBs, and effective storage management strategies. Additionally, explore index-organized tables for efficient data retrieval by primary key. Gain insights into analyzing table performance to support SQL query optimization.
E N D
Chapter 8 Advanced Table Management Oracle9i Database Administrator: Implementation and Administration
Objectives • Create tables with large object (LOB) columns and tables that are index-organized • Understand the tasks involved in table management • Use data dictionary views to find information about tables and their underlying structures Oracle9i Database Administrator: Implementation and Administration
Advanced Table Structures Table types covered: • Tables with LOB columns: • Store large amount of data in a single column of one row • Can define a separate segment for the LOB • Index-organized tables • Store data in primary key order • Best when queries nearly always use primary key to look up data in the table Oracle9i Database Administrator: Implementation and Administration
Tables with LOB Columns Types of LOB columns: • Internal LOB • Stored inside the database • Datatypes: BLOB, CLOB, NCLOB • External LOB • Stored outside the database • Datatypes: BFILE • Read-only Oracle9i Database Administrator: Implementation and Administration
Tables with LOB Columns Example: CREATE TABLE SOUNDBYTES (ALBUM_ID VARCHAR2(20), ARTIST VARCHAR2(40), CD_MUSIC BFILE, POSTER_SHOT BLOB ); External LOB Internal LOB Oracle9i Database Administrator: Implementation and Administration
Tables with LOB Columns New package for updating internal LOBs: • DBMS_LOB.LOADBLOBFROMFILE: Load data into BLOB from external file • DBMS_LOB.LOADCLOBFROMFILE: Load data into CLOB from external file Co Oracle9i Database Administrator: Implementation and Administration
Tables with LOB Columns Semantics of copying LOBs: • Internal LOB uses copy semantics: Copying a LOB from one row or column to another copies the contents • External LOB uses reference semantics: Copying a LOB from one row or column to another copies only a pointer to the original location of contents Co Oracle9i Database Administrator: Implementation and Administration
LOB Storage • Internal LOBs: • Default stored inline or out of line, by size • Always stored out of line with DISABLE STORAGE IN ROW • External LOBs: • LOB locator stored in the table • LOB data stored in operating system file Oracle9i Database Administrator: Implementation and Administration
LOB Storage Syntax: CREATE TABLE <tablename> (<other_column_specs>, <LOBcolumnname> <LOBdatatype>) LOB (<LOBcolumnname>) STORE AS <lobsegmentname> (TABLESPACE <tablespacename> ENABLE STORAGE IN ROW|DISABLE STORAGE IN ROW CHUNK <nn> STORAGE (INITIAL <nn> NEXT <nn> MAXEXTENTS UNLIMITED|<nn>) PCTVERSION <nn>|RETENTION LOGGING|NOLOGGING CACHE|NOCACHE); Oracle9i Database Administrator: Implementation and Administration
LOB Storage Syntax of storage for LOB: • STORE AS <lobsegmentname> • DISABLE / ENABLE STORAGE IN ROWS • CHUNK <nn> • PCTVERSION / RETENTION • LOGGING / NOLOGGING • CACHE / NOCACHE Oracle9i Database Administrator: Implementation and Administration
LOB Storage Example: Oracle9i Database Administrator: Implementation and Administration
Index-Organized Tables • Relational table with primary key • Sorted and stored in key order • Normal relational tables are heap-organized • Stored in b-tree structure Oracle9i Database Administrator: Implementation and Administration
Index-Organized Tables • Heap-organized versus Index-organized Oracle9i Database Administrator: Implementation and Administration
Index-Organized Tables • Example: Oracle9i Database Administrator: Implementation and Administration
Index-Organized Tables Syntax components: • ORGANIZATION INDEX • OVERFLOW • PCTTHRESHOLD • INCLUDING <col> • TABLESPACE <name> • STORAGE (<storage>) Oracle9i Database Administrator: Implementation and Administration
Index-Organized Tables Example: CREATE TABLE ZIPREFERENCES (ZIPCODE VARCHAR2(10) NOT NULL, CITY VARCHAR2(40) NOT NULL, STATE VARCHAR2(10) NOT NULL, PROVINCE VARCHAR2(10) NOT NULL, COUNTRY VARCHAR2(20) NOT NULL, CONSTRAINT ZIP_PK PRIMARY KEY(ZIPCODE)) ORGANIZATION INDEX TABLESPACE USERS PCTTHRESHOLD 20 OVERFLOW TABLESPACE USERS STORAGE (INITIAL 64K NEXT 32K MAXEXTENTS 50 PCTINCREASE 0); Oracle9i Database Administrator: Implementation and Administration
Overview of Table Management Types of changes: • Change the storage settings • Reorganize the table online • Drop columns • Truncate or drop the table Oracle9i Database Administrator: Implementation and Administration
Analyzing a Table Why analyze tables? • To give the optimizer up-to-date information for optimizing queries and other SQL commands • To give you information to help you decide which storage or column settings to change Oracle9i Database Administrator: Implementation and Administration
Analyzing a Table Oracle's optimizers: • Cost-based optimizer • Default • Uses table statistics • Rule-based optimizer • Older • Uses syntax rules only Oracle9i Database Administrator: Implementation and Administration
Analyzing a Table Examples • ANALYZE command: ANALYZE TABLE CUSTOMER ESTIMATE STATISTICS SAMPLE 1000 ROWS; • DBMS_STATS package: EXECUTE DBMS_STATS.GATHER_TABLE_STATS - ('CLASSMATE','CUSTOMER'); Oracle9i Database Administrator: Implementation and Administration
Adjusting Table Storage Structure Syntax of ALTER TABLE command: ALTER TABLE <schema>.<tablename> PCTFREE <nn> PCTUSED <nn> INITTRANS <nn> MAXTRANS <nn> STORAGE (NEXT <nn> PCTINCREASE <nn> MAXEXTENTS <nn>|UNLIMITED) ALLOCATE EXTENT SIZE <nn> DATAFILE <filename> DEALLOCATE UNUSED KEEP <nn> COMPRESS|NOCOMPRESS MOVE TABLESPACE <tablespacename> STORAGE (INITIAL <nn> NEXT <nn> PCTINCREASE <nn> MAXEXTENTS <nn>|UNLIMITED) COMPRESS|NOCOMPRESS ONLINE Oracle9i Database Administrator: Implementation and Administration
Adjusting Table Storage Structure Syntax components: • PCTFREE <nn> PCTUSED <nn> • INITTRANS <nn> MAXTRANS <nn> • STORAGE (...) • ALLOCATE EXTENT • DEALLOCATE UNUSED • COMPRESS • MOVE • ONLINE Oracle9i Database Administrator: Implementation and Administration
Adjusting Table Storage Structure Examples: • Deallocate unused space: ALTER TABLE HORSERACE DEALLOCATE UNUSED KEEP 50K; • Change STORAGE setting: ALTER TABLE CLASSIFIED_AD MOVE TABLESPACE USERS STORAGE (NEXT 56K); Oracle9i Database Administrator: Implementation and Administration
Reorganizing a Table DBMS_REDEFINITION package: • Redefine anything in the table • Keep old version of the table available during the change Oracle9i Database Administrator: Implementation and Administration
Reorganizing a Table DBMS_REDEFINITION steps: • Check the table – is it eligible • Create interim table with changes you want done • Start redefinition process • Finish redefinition process Each step (except #2) is a call to DBMS_REDEFINITION package Oracle9i Database Administrator: Implementation and Administration
Making Other Table Changes Syntax of the ALTER TABLE command: ALTER TABLE <schema>.<tablename> RENAME TO <newname> LOGGING|NOLOGGING MONITORING|NOMONITORING ENABLE|DISABLE ROW MOVEMENT CACHE|NOCACHE Oracle9i Database Administrator: Implementation and Administration
Making Other Table Changes ALTER TABLE components: • RENAME TO <newname> • LOGGING / NOLOGGING • MONITORING / NOMONITORING • ENABLE / DISABLE ROW MOVEMENT • CACHE / NOCACHE Oracle9i Database Administrator: Implementation and Administration
Making Other Table Changes Example: Modify monitoring and cache: ALTER TABLE EMPLOYEE MONITORING CACHE; Oracle9i Database Administrator: Implementation and Administration
Dropping, Adding, or Modifying a Column in a Table • Syntax for ALTER TABLE command: ALTER TABLE <schema>.<tablename> RENAME COLUMN <oldcolname> TO <newcolname> ADD (<colname> <datatype>, ... ) MODIFY (<colname> <datatype>, ... ) DROP (<colname>, <colname>,...)|COLUMN <colname> CASCADE CONSTRAINTS SET UNUSED (<colname>,<colname>,...)|COLUMN <colname> CASCADE CONSTRAINTS DROP UNUSED COLUMNS Note: Each of these clauses must be used alone in a single ALTER TABLE command. Oracle9i Database Administrator: Implementation and Administration
Dropping, Adding, or Modifying a Column in a Table Examples: • Rename a column: ALTER TABLE CH08SURGERY RENAME COLUMN PATIENT_FISRT_NAME TO PATIENT_FIRST_NAME; • Add a column: ALTER TABLE CH08SURGERY ADD (OUTCOME VARCHAR2(40), OPERATING_ROOM_NO CHAR(4)); • Change length of column: ALTER TABLE CH08SURGERY MODIFY (DOCTOR_NAME VARCHAR2(20)); Oracle9i Database Administrator: Implementation and Administration
Truncating and Dropping a Table TRUNCATE is fast: • No redo log entries • No undo records • Cannot rollback • Syntax: TRUNCATE TABLE <schema>.<tablename> DROP STORAGE|REUSE STORAGE Oracle9i Database Administrator: Implementation and Administration
Truncating and Dropping a Table DROP command: • Removes data and structure • Must drop constraints that use the dropped table • Example: DROP TABLE COPCAR CASCADE CONSTRAINTS; Oracle9i Database Administrator: Implementation and Administration
Querying Table-Related Data Dictionary Views Oracle9i Database Administrator: Implementation and Administration
Querying Table-Related Data Dictionary Views Oracle9i Database Administrator: Implementation and Administration
Querying Table-Related Data Dictionary Views Example: • Look at statistics gathered by monitoring tables: SELECT TABLE_NAME, TABLESPACE_NAME, MONITORING, BLOCKS, EMPTY_BLOCKS FROM USER_TABLES; Oracle9i Database Administrator: Implementation and Administration
Chapter Summary • LOB data types are either internal LOBS or external LOBS • New features in the DBMS_LOB package simplify loading LOB data • Internal LOB values larger than 4000 bytes are stored out of line in a LOB data segment • Oracle9i works with LOBs by reading and writing one chunk at a time • LOB data segments can also be used to store varray data Oracle9i Database Administrator: Implementation and Administration
Chapter Summary • Index-organized tables require a primary key and store data in order • Optionally split the column data into the main segment and an overflow segment • The ANALYZE command gathers statistics • The DBMS_STATS package also gathers statistics • Change a table’s storage parameters using the ALTER TABLE command Oracle9i Database Administrator: Implementation and Administration
Chapter Summary • DBMS_REDEFINITION restructures a table while keeping it online • ALTER TABLE can do these tasks: • Move table to different tablespace • Release unused space • Rename the table • Modify columns (add, change, drop) • TRUNCATE removes rows permanently Oracle9i Database Administrator: Implementation and Administration
Chapter Summary • TRUNCATE can release unused space or keep the space • DROP TABLE removes data and structure • CASCADE CONSTRAINTS if the table being dropped is connected by constraints to other tables Oracle9i Database Administrator: Implementation and Administration