Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4 Lecture 2 Advanced Table Management. Learning Objectives  Create tables with large object (LOB) columns and tables that are index-organized 

Similar presentations


Presentation on theme: "Week 4 Lecture 2 Advanced Table Management. Learning Objectives  Create tables with large object (LOB) columns and tables that are index-organized "— Presentation transcript:

1 Week 4 Lecture 2 Advanced Table Management

2 Learning 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

3 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

4 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

5 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

6 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

7 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

8 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

9 LOB Storage Syntax: CREATE TABLE (, ) LOB ( ) STORE AS (TABLESPACE ENABLE STORAGE IN ROW|DISABLE STORAGE IN ROW CHUNK STORAGE (INITIAL NEXT MAXEXTENTS UNLIMITED| ) PCTVERSION |RETENTION LOGGING|NOLOGGING CACHE|NOCACHE);

10 LOB Storage Syntax of storage for LOB: STORE AS DISABLE / ENABLE STORAGE IN ROWS CHUNK PCTVERSION / RETENTION LOGGING / NOLOGGING CACHE / NOCACHE

11 LOB Storage Example:

12 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

13 Index-Organized Tables Heap-organized versus Index- organized

14 Index-Organized Tables Example:

15 Index-Organized Tables Syntax components: ORGANIZATION INDEX OVERFLOW PCTTHRESHOLD INCLUDING TABLESPACE STORAGE ( )

16 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);

17 Overview of Table Management Types of changes: Collecting and Refreshing Statistics Change the storage settings Reorganize the table online Drop columns Truncate or drop the table

18 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

19 Analyzing a Table Oracle's optimizers: Cost-based optimizer Default Uses table statistics Rule-based optimizer Older Uses syntax rules only

20 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');

21 Adjusting Table Storage Structure Syntax of ALTER TABLE command: ALTER TABLE. PCTFREE PCTUSED INITTRANS MAXTRANS STORAGE (NEXT PCTINCREASE MAXEXTENTS |UNLIMITED) ALLOCATE EXTENT SIZE DATAFILE DEALLOCATE UNUSED KEEP COMPRESS|NOCOMPRESS MOVE TABLESPACE STORAGE (INITIAL NEXT PCTINCREASE MAXEXTENTS |UNLIMITED) COMPRESS|NOCOMPRESS ONLINE

22 Adjusting Table Storage Structure Syntax components: PCTFREE PCTUSED INITTRANS MAXTRANS STORAGE (...) ALLOCATE EXTENT DEALLOCATE UNUSED COMPRESS MOVE ONLINE

23 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);

24 Reorganizing a Table DBMS_REDEFINITION package: Redefine anything in the table Keep old version of the table available during the change

25 Reorganizing a Table DBMS_REDEFINITION steps: 1. Check the table – is it eligible 2. Create interim table with changes you want done 3. Start redefinition process 4. Finish redefinition process Each step (except #2) is a call to DBMS_REDEFINITION package

26 Making Other Table Changes Syntax of the ALTER TABLE command: ALTER TABLE. RENAME TO LOGGING|NOLOGGING MONITORING|NOMONITORING ENABLE|DISABLE ROW MOVEMENT CACHE|NOCACHE

27 Making Other Table Changes Example: Modify monitoring and cache: ALTER TABLE EMPLOYEE MONITORING CACHE;

28 Dropping, Adding, or Modifying a Column in a Table Syntax for ALTER TABLE command: ALTER TABLE. RENAME COLUMN TO ADD (,... ) MODIFY (,... ) DROP (,,...)|COLUMN CASCADE CONSTRAINTS SET UNUSED (,,...)|COLUMN CASCADE CONSTRAINTS DROP UNUSED COLUMNS Note: Each of these clauses must be used alone in a single ALTER TABLE command.

29 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));

30 Truncating and Dropping a Table TRUNCATE is fast: No redo log entries No undo records Cannot rollback Syntax: TRUNCATE TABLE. DROP STORAGE|REUSE STORAGE

31 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;

32 Querying Table-Related Data Dictionary Views

33

34 Example: Look at statistics gathered by monitoring tables: SELECT TABLE_NAME, TABLESPACE_NAME, MONITORING, BLOCKS, EMPTY_BLOCKS FROM USER_TABLES;

35 Lecture 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

36 Lecture 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

37 Lecture 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

38 Lecture 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


Download ppt "Week 4 Lecture 2 Advanced Table Management. Learning Objectives  Create tables with large object (LOB) columns and tables that are index-organized "

Similar presentations


Ads by Google