0% found this document useful (0 votes)
13 views

4-oracle-developer-tables-indexes-essentials-m4-slides

The document provides an overview of Oracle database storage options, including concepts like data blocks, tablespaces, and storage parameters such as PCTFREE and PCTUSED. It explains how Oracle manages data access, row migration, and the implications of high water marks on performance. Additionally, it discusses best practices for managing storage and optimizing database performance through proper configuration of storage parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

4-oracle-developer-tables-indexes-essentials-m4-slides

The document provides an overview of Oracle database storage options, including concepts like data blocks, tablespaces, and storage parameters such as PCTFREE and PCTUSED. It explains how Oracle manages data access, row migration, and the implications of high water marks on performance. Additionally, it discusses best practices for managing storage and optimizing database performance through proper configuration of storage parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Table Storage Options

David Berry
http://buildingbettersoftware.blogspot.com/
Introduction

students table
student_id first_name last_name phone

208718 Adam Brady 360-592-7886

208723 George Ward 216-952-0212

208728 Marcia Garcia 630-239-4325

208741 Walter Stumpf 419-500-5489

208749 Brenda Brown 979-859-7105


Module Outline

Database How Oracle High Water


Tablespaces
Blocks Reads Data Mark

PCTFREE and
Row Migration Freelists Row Chaining
PCTUSED
Database Blocks

Client
Oracle Instance Physical
Application
Storage

Applications are Oracle reads and writes


designed database blocks to disk
with rows in mind (2KB to 32KB in size)
A data block is like a shipping container

Diego Delso, Wikimedia Commons, License CC-BY-SA 3.0


What Is In a Block

Database Block
Block Header Block Header
• Block type information Row
• Object assignment
Row
• Row directory
Row
Row
Data Row
• Actual data for your table Free Space
Row
Row
Free Space
• Allows for updates to rows Row

Free Space
Oracle Storage Terminology

Smallest level of storage in Oracle


Data Block
Can be between 2-32KB (8KB common)

Contiguous allocation of blocks for storing a


Extent specific type of data

Set of extents allocated for a specific database


Segment structure
Storage Allocation

DEPARTMENTS (8 blocks) COURSES (8 blocks) MAJORS (8 blocks)

STUDENTS (8 blocks) COURSES (8 blocks) PROFESSORS (8 blocks)

STUDENTS (16 blocks)

STUDENTS (32 blocks)


How Oracle Performs Data Access

Index operation with


Full table scan
row lookup
Table
Data block

Data block

Data block

Data block

Data block

Data block

Data block

Data block

Data block

 Comparable to a linear search of an array Data block


Full Table Scan

 Oracle scans through every block in the table

Data block

Data block

Data block

Data block

Data block

Data block
Index Operation and Row Lookup

Index operation Row lookup in table

Oracle traverses the Oracle looks up the rows


index tree to find in the table by ROWID
matching keys from index
Why Does Some situations affect one data access path,
This Matter? but not the other
Table Data and Sort Order

Data in a table has no implied order


• Oracle will insert rows wherever they fit
• Physical order on disk != Order data was inserted in

Data returned from queries is not sorted


• Order of results is not order data was inserted
• Two different executions can return same results in different order
Tablespace Introduction

Tables and indexes Data files


Tablespace

datafile

datafile

datafile
Tablespace Block Size

Between 2 KB and 32 KB
(8 KB is a typical size)

Useful for estimating number of rows per block

Need to know to avoid row chaining


Segment Space Management

Manual Segment Automatic Segment


Space Management Space Management
(MSSM) (ASSM)

• Original space management • Introduced in Oracle 9i


implementation • You specify value for PCTFREE
• You are responsible for managing • Oracle manages all other
all parameters for space parameters
management
Oracle Contains Multiple Tablespaces

SYSTEM and • Data about your Oracle instance


SYSAUX • Reserved for use by Oracle

• Data in temporary tables


TEMP • Sorts and joins that do not fit in memory

• Data for your applications


User Tablespaces • May be one per application, group of
applications
Default Tablespace

Some objects may be


Every user has a
created in an
default tablespace
alternate tablespace
Specifying a Tablespace for a Table

CREATE TABLE course_enrollments


(
course_enrollment_id NUMBER NOT NULL,
course_offering_id NUMBER(20) NOT NULL,
student_id NUMBER(10) NOT NULL,
grade_code VARCHAR2(1) NULL,
CONSTRAINT pk_course_enrollments
PRIMARY KEY (course_enrollment_id),
CONSTRAINT fk_enrollments_offerings
FOREIGN KEY (course_offering_id)
REFERENCES course_offerings (course_offering_id),
CONSTRAINT fk_enrollments_student
FOREIGN KEY (student_id) REFERENCES students (student_id),
CONSTRAINT fk_enrollments_grades
FOREIGN KEY (grade_code) REFERENCES grades (grade_code)
)
TABLESPACE student_data;
High Water Mark
Highest numbered block that has
ever contained data in a table
High Water Mark

Blocks that have contained data

High Water Mark High Water Mark


High Water Mark – Deleting Rows

Blocks that have contained data

High Water Mark


High Water Mark – ASSM

Formatted data blocks Available for new rows

“Low” High “High” High


Water Mark Water Mark
High Water Mark Impacts

During a full table scan,


If many of these blocks
Oracle reads all blocks
are empty, this is a lot of
below the high water
wasted work
mark
One row of data

Empty block

Empty block

Empty block

Empty block

Empty block

Empty block

Empty block

Empty block

Empty block

Empty block
Full table scan reads all blocks below the high water mark

Empty block

Empty block
High Water Mark and Full Table Scans

High Water Mark


High Water Mark Impacts

Should I Worry?
• Generally, no
• Natural to have a few empty blocks below the high water mark

One scenario to monitor


• Tables that have a large number of rows inserted and deleted
• Subsequent processing uses many fewer records
• Consider TRUNCATE instead of DELETE
A data block is “full”

What does this really mean?


Storage Implications of Updating Rows

student_id first_name last_name phone email_address major_id

208718 Adam Brady 360-592-7886 [email protected] 3

208723 George Ward 216-952-0212 [email protected] 5

188728 Marcia Garcia 630-239-4325 [email protected] 8

208741 Walter Stumpf NULL [email protected] 12

UPDATE students
SET last_name = ‘Anderson’
WHERE student_id = 188728;

UPDATE students
SET phone = ‘419-500-5489’
WHERE student_id = 208741;
PCTFREE Introduction

Data block
Block Header
Row data

Row data

Row data

Row data

Row data

Row data
No space is left for row to
New data
expand when the row is
updated
PCTFREE Introduction

Data block
Block header
Row data

Row data

Row data

Row data

Space is available New data Amount of space left is


for row to expand controlled by PCTFREE
Free Space parameter
on update
PCTFREE Example

Data block
Block header
Row data

Row data

Row data

Row data

Row data Oracle considers


PCTFREE = 20
the block full
Free Space

Used to support rows


increasing in size
Setting PCTFREE

• Default value is 10 percent

• Specify using PCTFREE in storage clause


CREATE TABLE students
(
student_id NUMBER(10) NOT NULL,
first_name VARCHAR2(40) NOT NULL,
middle_name VARCHAR2(40) NOT NULL,
last_name VARCHAR2(40) NOT NULL,
gender VARCHAR2(1) NOT NULL,
street_address VARCHAR2(50) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(2) NOT NULL,
email VARCHAR2(80) NOT NULL,

)
PCTFREE 20;
PCTFREE

Applies to both ASSM an MSSM tablespaces

Need to understand table data, update patterns to set


PCTFREE
PCTFREE too low – row migrations will occur on
updates
PCTFREE too high – space will be wasted in
each block
PCTUSED

Only applies to MSSM tablespaces Data block


Block header
Row data
Threshold at which Oracle will once
again allow inserts into the block Free
RowSpace
data

Free
RowSpace
data

RowSpace
Free data

Row data

Free Space
PCTUSED Example

Data block
Block header
Row data

Row data Rows can again


PCTUSED = 40
be added to the
(default value) Row data
block
Row data

Row data Oracle considers


PCTFREE = 20
the block full
Free Space
Setting PCTUSED

CREATE TABLE students


(
student_id NUMBER(10) NOT NULL,
first_name VARCHAR2(40) NOT NULL,
middle_name VARCHAR2(40) NOT NULL,
last_name VARCHAR2(40) NOT NULL,
gender VARCHAR2(1) NOT NULL,
street_address VARCHAR2(50) NOT NULL,
city VARCHAR2(30) NOT NULL,
state VARCHAR2(2) NOT NULL,
email VARCHAR2(80) NOT NULL,

)
PCTUSED 50;
When Does Row Migration Occur?

Data block
Block header
Row data

Oracle will migrate the


Row data entire row to a
different data block
Row data

Row data

Free Space

New data

Updated data will


not fit in available
free space of block
Row Migration

Original data block New data block


Block header Block header
Row data Row data
(New Location)
Row data
Row data
Free Space
Row data

Pointer Row data

Free Space
Free Space

Pointer points from


old location to new
location
Impacts of Row Migration

Row lookup operations will now require two IO’s

Full table scans do not incur a penalty

Row can be migrated multiple times but


Oracle updates the original pointer
Types of Row Migration

PCTFREE Too Low Natural Row Migration

• Insufficient space for rows to • Natural for a small percentage of


expand into rows to migrate
• Many rows will be migrated • Nothing to worry about
through normal updates
• Something to be avoided
Detecting Row Migration

Extra work is coming from row migrations?


SELECT name, value
FROM v$sysstat
WHERE name IN ('table fetch continued row',
'table fetch by rowid');

Migrated and chained rows per table


SELECT table_name, chain_cnt, num_rows, last_analyzed
FROM user_tables;
PCTFREE too low
Row migrations will occur
Practical Guidance
on PCTFREE
PCTFREE too high
Waste space with partially filled blocks
Using a Low PCTFREE Value

• No update activity (inserts only)


Table Characteristics
• Update will not affect the size of a row

• Logging tables
Examples
• Some data warehouse applications
Using a High PCTFREE Value

• Data added to rows incrementally


Table Characteristics
• Updates increase row size

• Tables seeded with initial value, other


Examples
data added over time
A Sample Scenario

50 bytes 200 bytes 300 bytes 500 bytes


Know how your application inserts
PCTFREE and updates data
Considerations Don’t be afraid to run a test
Freelists

List of blocks that have free space that Oracle


Definition can use to insert new rows into

Freelists are automatically managed


ASSM
Major advantage of ASSM

Default number of freelists is one


MSSM
Can be a bottleneck for concurrent DML
Setting FREELISTS at Table Creation

CREATE TABLE course_enrollments


(
course_enrollment_id NUMBER NOT NULL,
course_offering_id NUMBER(20) NOT NULL,
student_id NUMBER(10) NOT NULL,
grade_code VARCHAR2(1) NULL,

)
STORAGE (FREELISTS 12);
Changing Freelists on an Existing Table

ALTER TABLE course_enrollments


STORAGE (FREELISTS 12);

 Correct table where freelists was set incorrectly

 Perform a bulk load in parallel


Row Chaining

Database Block Database Block


Block Header Block Header

Yet even more


data for that one
Data for one single, row with lots of data
very long row

Free Space

Two IO operations will be required to


read this row from disk
Table With Row Chaining

Value between 2 – 32 KB
Tablesapce
block size Assume for example 4 KB

CREATE TABLE products


(
product_id NUMBER(8) NOT NULL,
Sample Table sku VARCHAR2(32) NOT NULL,
name VARCHAR2(128) NOT NULL,
Definition description VARCHAR2(4000) NOT NULL,
tech_specs VARCHAR2(4000) NOT NULL
);
Detecting Chained Rows

Tables at risk for row chaining


• Tables with lots of columns ( >100 columns)
• Tables with large sized CHAR/VARCHAR2 columns

Statistics on chained and migrated rows


-- Gather statistics on the table
EXEC DBMS_STATS.GATHER_TABLE_STATS(
ownname => ‘<<Table Owner Name>>’,
tabname => ‘<<Table Name>>’);

-- Chain count is sum of chained and migrated rows


SELECT table_name, chain_cnt, num_rows, last_analyzed
FROM user_tables;
Identifying Chained Rows

-- Script from $ORACLE_HOME/rdbms/admin/utlchain.sql


CREATE TABLE chained_rows
(
owner_name varchar2(128),
table_name varchar2(128),
cluster_name varchar2(128),
partition_name varchar2(128),
subpartition_name varchar2(128),
head_rowid rowid,
analyze_timestamp date
);

-- Analyze the table


ANALYZE TABLE <<table name>> LIST CHAINED ROWS;
Solutions To Row Chaining

Use a sufficient block size for your table data

Estimate row sizes for each table from logical data model

Specify the tablespace block size based on these estimates

Move tables with large rows to a new tablespace


Vertically Partition Tables

product_id sku name summary product_description technical_specs product_review

400001 HD-100 256 GB Solid State …. …. …. ….


Hard Drive
600483 GC-862 GraphicsStar! …. …. …. ….
Graphics Card
7839390 MO-101 27” Flat Screen ….. …. …. ….
Monitor – 1080P
8920201 MS-173 Wireless Mouse ….. …. …. ….
Vertically Partition Tables

product_id sku name summary product_description

400001 HD-100 256 GB Solid …. ….


State Hard Drive
600483 GC-862 GraphicsStar! …. ….
Graphics Card
7839390 MO-101 27” Flat Screen ….. ….
Monitor – 1080P
8920201 MS-173 Wireless Mouse ….. ….

product_id technical_specs product_review

400001 …. ….
Foreign key
600483 …. ….
between tables
7839390 …. ….

8920201 …. ….
Data storage using data blocks

How Oracle accesses data

Tablespaces

Module Summary PCTFREE

Row Migration

Freelists

Row Chaining
Oracle Database Concepts Guide
http://bit.ly/Ora12cDatabaseConcepts

You might also like