clase 11 manejo tablas modificada

28
Managing Tables

Upload: titiushko-jazz

Post on 22-Jan-2018

245 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Clase 11 manejo tablas   modificada

Managing Tables

Page 2: Clase 11 manejo tablas   modificada

Objectives

After completing this lesson, you should be able todo the following:• Create tables using appropriate storage settings• Control the space used by tables• Analyze tables to check integrity and migration• Retrieve information about tables from the data

dictionary• Convert between different formats of ROWID

Page 3: Clase 11 manejo tablas   modificada

Storing User Data

Regular

table

Cluster

Partitioned

table

Index-organized

table

Page 4: Clase 11 manejo tablas   modificada

Structure of a Row

Database block

Row header

Column length

Column value

Page 5: Clase 11 manejo tablas   modificada

Oracle Data Types

CHAR(N), NCHAR(N)

VARCHAR2(N),

NVARCHAR2(N)

NUMBER(P,S)

DATE

RAW(N)

BLOB, CLOB,

NCLOB, BFILE

LONG, LONG RAW

ROWID, UROWID

VARRAY

TABLE

REF

Data type

Built-inUser-defined

Scalar RelationshipCollection

Page 6: Clase 11 manejo tablas   modificada

Data Types for Storing Large Objects

LONG, LONG RAW

Single column per table

Up to 2 gigabytes

SELECT returns data

Data stored in-line

No object type support

Sequential access to chunks

LOB

Multiple columns per table

Up to 4 gigabytes

SELECT returns locator

Data stored in-line or out-of-line

Supports object types

Random access to chunks

Page 7: Clase 11 manejo tablas   modificada

ROWID Data Type

• Unique identifier for a row • Used to locate a row

OOOOOO BBBBBBFFF RRR

Data object

number

Relative file

number

Row numberBlock number

ROWID Format

Page 8: Clase 11 manejo tablas   modificada

Restricted ROWID

• Can identify rows within a segment• Needs less space

BBBBBBBB FFFFRRRR

Block number Row number File number

. .

Page 9: Clase 11 manejo tablas   modificada

Collections

• Collections are objects that contain objects.• VARRAYs are ordered sets of elements containing

a count and a limit.• Nested tables are tables with a column or

variable of the TABLE data type.

VARRAY Nested

table

Page 10: Clase 11 manejo tablas   modificada

Creating a Table

CREATE TABLE employee(

id NUMBER(7),

last_name VARCHAR2(25),

dept_id NUMBER(7))

PCTFREE 20 PCTUSED 50

STORAGE(INITIAL 200K NEXT 200K

PCTINCREASE 0 MAXEXTENTS 50)

TABLESPACE data;

Page 11: Clase 11 manejo tablas   modificada

Temporary Tables

• The rows are private to the session.

• Tables retain data only for the duration of a transaction or session.

• DML locks are not acquired on the data.• DMLs do not generate redo logs.

ON COMMIT PRESERVE ROWS;

CREATE GLOBAL TEMPORARY TABLE employee_temp

AS SELECT * FROM employee;

Page 12: Clase 11 manejo tablas   modificada

Creating a Table: Guidelines

• Use a few standard extent sizes for tables to reduce tablespace fragmentation.

• Use locally managed tablespaces to avoid fragmentation.

• Use the CACHE clause for frequently used, small tables.

Page 13: Clase 11 manejo tablas   modificada

Setting PCTFREE and PCTUSED

• Compute PCTFREE

(Average Row Size - Initial Row Size) * 100

Average Row Size

• Compute PCTUSED

Average Row Size * 100

100 - PCTFREE -

Available Data Space

Page 14: Clase 11 manejo tablas   modificada

Row Migration and Chaining

Before update After update

Pointer

Page 15: Clase 11 manejo tablas   modificada

Changing Storage and Block Utilization Parameters

ALTER TABLE summit.employee

PCTFREE 30

PCTUSED 50

STORAGE(NEXT 500K

MINEXTENTS 2

MAXEXTENTS 100);

Page 16: Clase 11 manejo tablas   modificada

Manually Allocating Extents

ALTER TABLE summit.employee

ALLOCATE EXTENT(SIZE 500K

DATAFILE ‘/DISK3/DATA01.DBF’);

Page 17: Clase 11 manejo tablas   modificada

Nonpartitioned Table Reorganization

ALTER TABLE employee

MOVE TABLESPACE data1;

• Moves data into a new segment while preserving indexes, constraints, privileges, and so on, on the table

• Is being used to move a table to a different tablespace or to reorganize extents

Page 18: Clase 11 manejo tablas   modificada

Free space

after delete

Unused block

Extent ID 0 1 2 3 4

Extent ID 0 1 2 3 4

Used block

High-Water MarkAfter inserts:

After deletes:High-water

mark

Page 19: Clase 11 manejo tablas   modificada

Finding the High-Water Mark: DBMS_SPACE.UNUSED_SPACE

Extent ID 0 1 2 3 4

High-water mark

LAST_USED_EXTENT_FILE_ID,

LAST_USED_EXTENT_BLOCK_ID

TOTAL_BLOCKS

UNUSED_BLOCKS

Page 20: Clase 11 manejo tablas   modificada

Free space

after delete

Unused block

Before

deallocation

Used block

Deallocation of Unused Space

ALTER TABLE summit.employee

DEALLOCATE UNUSED;

After

deallocation

High-water

mark

Extent ID 43210

43210Extent ID

Page 21: Clase 11 manejo tablas   modificada

Truncating a Table

TRUNCATE TABLE summit.employee;

Extent ID 0 1

High-water

mark

Free space

Page 22: Clase 11 manejo tablas   modificada

Dropping a Table

DROP TABLE summit.department

CASCADE CONSTRAINTS;

Page 23: Clase 11 manejo tablas   modificada

Dropping a Column

ALTER TABLE employee

DROP COLUMN comments

CASCADE CONSTRAINTS CHECKPOINT 1000;

Remove a column from a table

• This removes the column length and data from each row, freeing space in the data block.

• Dropping a column in a large table takes a considerable amount of time.

Page 24: Clase 11 manejo tablas   modificada

Using the UNUSED Option

ALTER TABLE orders

SET UNUSED COLUMN comments

CASCADE CONSTRAINTS;

• Drop unused columns

ALTER TABLE orders

DROP UNUSED COLUMNS CHECKPOINT 1000;

• Mark a column as unused

• Continue to drop column operation

ALTER TABLE orders

DROP COLUMNS CONTINUE CHECKPOINT 1000;

Page 25: Clase 11 manejo tablas   modificada

Retrieving Table InformationDBA_OBJECTSOWNER

OBJECT_NAME

OBJECT_ID

DATA_OBJECT_ID

CREATED

DBA_SEGMENTSOWNER

SEGMENT_NAME

TABLESPACE_NAME

HEADER_FILE

HEADER_BLOCK

DBA_TABLESOWNER

TABLE_NAME

PCT_FREE

PCT_USED

INITIAL_EXTENT

NEXT_EXTENT

MIN_EXTENTS

MAX_EXTENTS

PCT_INCREASE

CACHE

BLOCKS

EMPTY_BLOCKS

CHAIN_CNT

TEMPORARY

DURATION

Page 26: Clase 11 manejo tablas   modificada

DBA_EXTENTS

• OWNER

• SEGMENT_NAME

• EXTENT_ID

• FILE_ID

• BLOCK_ID

• BLOCKS

Retrieving Extent Information

Page 27: Clase 11 manejo tablas   modificada

DBMS_ROWID Package

Function Name

ROWID_CREATE

ROWID_OBJECT

ROWID_RELATIVE_FNO

ROWID_BLOCK_NUMBER

ROWID_ROW_NUMBER

ROWID_TO_ABSOLUTE_FNO

ROWID_TO_EXTENDED

ROWID_TO_RESTRICTED

Description

Creates a ROWID from individual components

Returns the object identifier for a ROWID

Returns the relative file number for a ROWID

Returns the block number for a ROWID

Returns the row number for a ROWID

Returns the absolute file number for a ROWID

Converts a ROWID from restricted to extended

Converts a ROWID from extended to restricted

Commonly used functions:

Page 28: Clase 11 manejo tablas   modificada

Summary

In this lesson, you should have learned how to:• Create a table with appropriate storage and

block utilization parameters• Control table storage• Use the DBMS_ROWID package